- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2015 05:21 PM
Hello all,
I have a client script that makes an Ajax Call through a script include - exactly as described in the wiki, here: GlideAjax - ServiceNow Wiki using client-side code similar to the example on that page:
var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name','helloWorld');
ga.addParam('sysparm_user_name',"Bob");
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
However, 'answer' in my case is a JSON object. The script include part is working fine, but I'm struggling to 'get at' the contents of the JSON object client-side. What is the simplest way of doing this? I want to just be able to do something like this...
var myVariable1 = answer.variable1;
var myVariable2 = answer.variable2;
but I realise it's not that simple. I've tried to piece this together from other answers to similar posts but I'm missing something.
I've tried something like:
var parser = new JSON();
var parsed = parser.decode(answer);
and:
var parser = new JSONParser();
var parsed = parser.jsonParse(answer);
and:
answer = JSON.parse(answer);
but I've not managed to get any of these working.
Thanks for any pointers,
Jamie.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2015 06:23 PM
That example and most examples will return a XML payload;
If you do a console.log(ga.requestObject.response); you'll see this;
"<?xml version="1.0" encoding="UTF-8"?><xml answer="Hello Bob!" sysparm_max="15" sysparm_name="helloWorld" sysparm_processor="HelloWorld"/>"
If you want to handle the response to be some JSON, you would need to modify the server side code, and have it pass it down stringified/serialized;
Here's an example;
Server side Script Include;
var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
helloWorld: function() {
return "Hello " + this.getParameter('sysparm_user_name') + "!";
},
helloWorld2: function() {
var j = new JSON();
var response = {
sysparm_user_name: this.getParameter('sysparm_user_name'),
answer: "Hello " + this.getParameter('sysparm_user_name') + "!"
};
return j.encode(response);
},
type: 'HelloWorld'
});
Client side code (you can even paste it in your console (F12) and it should work. Did for me;
var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name','helloWorld2');
ga.addParam('sysparm_user_name',"Bob");
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
console.log('answer: ' + answer);
console.log('answer.sysparm_user_name: ' + answer.sysparm_user_name);
}
Output I get from that client side code;
answer: [object Object]
answer.sysparm_user_name: Bob

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2016 08:04 PM
Glad it helped!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2016 07:30 AM
I've got a followup question for you...
If I am calling the script include from a UI Script, do I need to do anything different?
This UI Script function get binded to a UI Page button. The button works. I just can't get the Glide Ajax to work right.
testbutton: function (event) {
var ga = new GlideAjax("MyCustomAjax");
ga.addParam("sysparm_name", "newinc");
ga.getXML(testbuttonResponse);
function testbuttonResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON();
var nr = answer.newinc;
}
var loc = "incident.do?sys_id=" + nr;
if (nr) {
window.location = loc;
}
},
Script Include
var MyCustomAjax = Class.create();
MyCustomAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
newinc: function() {
var desc = this.getParameter('sysparm_desc');
var gr = new GlideRecord('incident');
gr.newRecord();
var newinc = gr.insert();
ajx = {};
ajx.newinc = newinc;
var json = new JSON();
var data = json.encode(ajx);
return data;
},
type: 'MyCustomAjax'
});
(To test my script include, I transfer it to an onLoad client script), and it works.)
The returned "newinc" value in the JSON should be the sys ID of the newly created INC, but I keep getting nothing back.