Script Include returns a string instead of an array

Snow Tomcal
Tera Expert

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 🙂

1 ACCEPTED SOLUTION

AB3
Tera Expert

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.

View solution in original post

4 REPLIES 4

Peter Bodelier
Giga Sage

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.

bc884e28-96d8-4716-a534-8503b4eff6ab.jpeg

9ebe756c-cdf2-4279-9f68-7d659e8c6ff0.jpeg

Peter Bodelier
Giga Sage

@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.

AB3
Tera Expert

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.