- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2014 01:47 PM
Hi All,
I'm trying to pass an array as a parameter for a GlideAjax call, the information is getting there but it's not working as expected, any ideas why?
Client Script | Script Include Ajax |
---|---|
var arr = [ ["A", "1", true], ["B", "2", true], ["C", "3", true] ];
var ga = new GlideAjax('ajaxClass'); ga.addParam('sysparm_name','methodName'); ga.addParam('sysparm_array', arr); ga.getXMLWait(); | var ajaxClass = Class.create();
ajaxClass.prototype = Object.extendsObject(AbstractAjaxProcessor, { methodName: function() { try { var arr = this.getParameter('sysparm_array'); gs.log(arr); // Works, printed comma separated gs.log(arr.length); // Doesn't Work gs.log("Done"); // Doesn't Execute } catch(e) { gs.log("ERROR"); // Doesn't Execute } gs.log("Return Stuff"); // Doesn't Execute return "Complete"; } }); |
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2014 05:38 PM
You cannot pass an Object from Client Script yo Script Include.
This is because - WHen you pass an Object, it will get flattened by Mozilla Rhino that interprets all your Javascript calls into Java Calls.
What you need to do is this: First encode this Array as a string, and pass it. For example - You can convert an array to a string like this(on the client side) :
var arr = [
["A", "1", true],
["B", "2", true],
["C", "3", true]
];
var strArr = Object.toJSON(arr); // Now you have encoded your array.
For more info on the Object.toJSON method, see the Prototype documentation : http://prototypejs.org/learn/json
Pass strArr in place of arr.
Now, on the server side, convert the string into an array and use it.
here is the server side code :
var str = this.getParameter('sysparm_array');
var arr = new JSON().decode(str);// gives you an array.
You can now use arr as an array you used on the Client Side.
Now coming to your array, it's not really an Array. As Array in Javascript can have a collection of Objects but not collection of arrays. Objects are nothing but key value pairs. Henceforth use either an Array of Objects, or an object to convert it to a string and move it to the Server side.
Josh.nerius also told the same thing, but using different functionality.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2014 03:37 PM
Shane,
As far as I can tell, objects are converted to strings before they are sent as GlideAjax parameters. As a result, by the time your request is sent to the server, it's already in a string format and the data you're seeing (something like 'A,1,true,B,2,true,C,3,true') cannot be processed as an array. Here are the key/pair values POSTed by the sample code you provided (use Chrome developer tools to view these):
- sysparm_processor:ajaxClass
- sysparm_name:methodName
- sysparm_array:A,1,true,B,2,true,C,3,true
- sysparm_synch:true
Since the data is already a string (it's essentially being "flattened") by the time it's processed in the Script Include, it no longer resembles your original data structure and cannot processed as an array of arrays.
Depending on how complex your data will be, you could try converting and passing the data to your GlideAjax call as a JSON string and then parsing this string server-side. You can use the JSONParser Script Include to parse JSON strings - see JSONParser - ServiceNow Wiki for more details. For example, you'd want to pass a string that looks like this in your parameter:
var arr = "[['A', '1', true],['B', '2', true],['C', '3', true]]";
In your server side code, you can then use something like:
var parser = new JSONParser();
var arrayString = this.getParameter('sysparm_array');
var arrayParsed = parser.parse(arrayString);
The arrayParsed variable will now be a first class JavaScript object that will behave the way you expect it to.
A challenge with this approach will be converting your client side objects into JSON strings. You can look into "JSON.stringify()", but this is only available in modern browsers and is not a ServiceNow method so in short, is probably not directly supported (at least not that I'm aware of, someone please correct me if I'm wrong on this). Alternatively, you could write your own function to iterate over the data on the client side to build the JSON string(s).
I'm very interested to see if anyone else has additional input on this. Best of luck!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2014 05:38 PM
You cannot pass an Object from Client Script yo Script Include.
This is because - WHen you pass an Object, it will get flattened by Mozilla Rhino that interprets all your Javascript calls into Java Calls.
What you need to do is this: First encode this Array as a string, and pass it. For example - You can convert an array to a string like this(on the client side) :
var arr = [
["A", "1", true],
["B", "2", true],
["C", "3", true]
];
var strArr = Object.toJSON(arr); // Now you have encoded your array.
For more info on the Object.toJSON method, see the Prototype documentation : http://prototypejs.org/learn/json
Pass strArr in place of arr.
Now, on the server side, convert the string into an array and use it.
here is the server side code :
var str = this.getParameter('sysparm_array');
var arr = new JSON().decode(str);// gives you an array.
You can now use arr as an array you used on the Client Side.
Now coming to your array, it's not really an Array. As Array in Javascript can have a collection of Objects but not collection of arrays. Objects are nothing but key value pairs. Henceforth use either an Array of Objects, or an object to convert it to a string and move it to the Server side.
Josh.nerius also told the same thing, but using different functionality.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2014 12:27 AM
Is there a way to do it the other way around? Meaning... passing a string from the client to the server (of course... that's not an issue ); but then *where i'm having trouble* is to retrieve an array from the server to the client. Should this line also be valid on the client side: var arr = new JSON().decode(str); if i previously encoded the array with Object.toJSON(arr); at the server side?
Just for context, I'm using GlideAjax with a synchronous call within an OnSubmit method; and i also tried this great article: Let's do some JSON today, but it was getting an error on the following line: var answer = response.responseXML.documentElement.getAttribute("answer");
Thanks in advanced for your help!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2014 05:24 AM
Try:
var arr = str.evalJSON(true);
The true parameter tells the parser to sanitize the string, checking for malicious / dangerous content.
Hope this helps!
Josh