Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Ajax and JSON

Jamsta1912
Tera Guru

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.

1 ACCEPTED SOLUTION

Jace Benson
Mega Sage

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


View solution in original post

6 REPLIES 6

Jace Benson
Mega Sage

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


Thank you Jace - that's exactly what I was after. Got this working fine now


Awesome! One helpful tip I can mention is this: when you're on the client-side, you have to use the native JavaScript JSON functions (JSON in JavaScript ), while when you're on the server-side, you have to use the Script Includes that ServiceNow has defined.


Thank you Jace, it works