- 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-16-2014 07:06 AM
Thanks guys,
I figured that's what was happening, but my method failing on calling 'arr.length' was throwing me off, especially since it wasn't throwing an exception, and since it is a string it should have work but been not what I expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2017 03:10 AM
Hi All,
Am trying to parse a json response which is obtained from REST APIand want to store parsed data to my user table,and below is my code which am running in scheduled job... but am getting undefined when i try to print the values obtained... how to achieve it?
-------------------------------------------------------------------------------------------------------------------------------------------------------------
try {
var r = new sn_ws.RESTMessageV2('Test', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
// gs.print(responseBody);
var parser = new global.JSON();
var parsed = parser.decode(responseBody);
gs.print(parsed.length);
var responsefinal = parsed.split('[')[1];
var userIDArray = [];
var nameArray = [];
for(var i=0;i<10;i++){
gs.print(responsefinal[i].UserID);
userIDArray.push(responsefinal[i].UserID.toString());
nameArray.push(responsefinal[i].Name.toString());
gs.print(responsefinal[i].Name);
}
}
catch(ex) {
var message = ex.getMessage();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
Please help me with this...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 03:53 AM - edited 09-04-2023 04:00 AM
You can make it work in two steps. all you need to do in your script include is:
arr = String(arr); //Java object to string.
arr = arr.split(","); //split by commas
OR
Change your AJAX call. Please see below is example
var ga = new GlideAjax("myUtility");
ga .addParam("sysparm_name", "myMethod");
ga .addParam("sysparm_myArray", JSON.stringify(['A','B']));
ga .getXML(callback);
Then in the script include you just have to do this:
var myArray = this.getParameter('sysparm_myArray');
var myArray = JSON.parse(myArray);