- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2023 06:41 AM
Hi All,
I have a script include that query a table and fill an array with all the records from the query.
When I try to use the array from the client script (using glide Ajax) the type of the response is string instead of array.
Does anyone knows what can be the cause to this problem?
Thanks in advance 🙂
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2023 07:03 AM
The issue you're facing is due to the fact that GlideAjax returns data as a string. When you're returning an array from a Script Include, it's being converted to a string format. To use this data as an array in your client script, you need to parse it back into an array format. Here are the steps to resolve this issue:
1. In your Script Include, convert the array to a JSON string before returning it. You can use the JSON.stringify() method for this.
Example: javascript
var myArray = ['item1', 'item2', 'item3'];
var jsonString = JSON.stringify(myArray);
return jsonString;
2. In your GlideAjax callback function, parse the JSON string back into an array using the JSON.parse() method. Example: javascript
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'myFunction');
ga.getXML(Answer);
function Answer(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var myArray = JSON.parse(answer); // Now you can use myArray as an array
}
Remember:
- Always convert your data to a string format before returning it from a Script Include.
- Always parse your data back into its original format in your GlideAjax callback function.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2023 06:43 AM
Hi @Snow Tomcal,
Can you please share your script?
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2023 06:54 AM
 
 

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2023 06:59 AM
@Snow Tomcal
In your client script you will need to convert the string to an array before you start your for loop.
Use something like var answer = response.split(',') or var answer = JSON.parse(response) and then use answer as variable in your for loop instead of response.
the getXMLAnswer can only return a string, not an object or array.
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2023 07:03 AM
The issue you're facing is due to the fact that GlideAjax returns data as a string. When you're returning an array from a Script Include, it's being converted to a string format. To use this data as an array in your client script, you need to parse it back into an array format. Here are the steps to resolve this issue:
1. In your Script Include, convert the array to a JSON string before returning it. You can use the JSON.stringify() method for this.
Example: javascript
var myArray = ['item1', 'item2', 'item3'];
var jsonString = JSON.stringify(myArray);
return jsonString;
2. In your GlideAjax callback function, parse the JSON string back into an array using the JSON.parse() method. Example: javascript
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'myFunction');
ga.getXML(Answer);
function Answer(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var myArray = JSON.parse(answer); // Now you can use myArray as an array
}
Remember:
- Always convert your data to a string format before returning it from a Script Include.
- Always parse your data back into its original format in your GlideAjax callback function.