why we cant return object through script include ajax

adityahubli
Tera Contributor

hey when i share object/array  from ajax script include at that time it cant receive whole object/array at client side , to resolve that i have to first convert object/array in string using JSON.stringify() method and at client side i need to parse the receiving string , why we cant send object directly without converting into string 

 

 

var scriptIncludeMultiRecord = Class.create();
scriptIncludeMultiRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
multiRecord:function()
{
var arr=[];
var gr=new GlideRecord('incident');
var user=this.getParameter('sysparm_user');
    gr.addQuery('caller_id',user);
gr.query();
while(gr.next())
{
var obj={};
obj.number=gr.number.toString();
obj.description=gr.description.toString();
obj.short_description=gr.short_description.toString();
arr.push(obj);
 
}
return JSON.stringify(arr);     <---------------------------------------------------------------------------
},
    type: 'scriptIncludeMultiRecord'
});
3 REPLIES 3

Astik Thombare
Tera Sage

Hi @adityahubli 

 

When you use GlideAjax (Script Include ↔ Client Script), you’re not just calling a JavaScript function directly — you’re actually making an HTTP request from the browser to the server and back.

HTTP (and GlideAjax built on top of it) only knows how to send/receive data as text (string). It doesn’t understand native JavaScript objects or arrays

 

That’s why:

On the server side, even if you build an object/array, you must convert it into a string (JSON.stringify) so it can travel over the network as plain text.

On the client side, you take that string and turn it back into a usable JavaScript object (JSON.parse).

 

If ServiceNow allowed sending native objects directly, the browser and server would have to share the same memory space and JavaScript runtime — which isn’t possible in a request/response model. The only universal, lightweight way to transfer structured data is via a serialized string format (like JSON or XML).

 

👉 So the short answer: We send only strings because GlideAjax works over HTTP, and HTTP transfers data as text. Objects/arrays must first be serialized (stringified) to be transmitted.

 

If my answer helped you, please consider marking it as Correct and giving it a 👍 Helpful — it might help other members in the community too!

 

Thanks,
Astik T
ServiceNow Community Rising Star 2025 🌟

Astik Thombare
Tera Sage

Hi @adityahubli ,

 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Thanks,
Astik T
ServiceNow Community Rising Star 2025 🌟

 

Ankur Bawiskar
Tera Patron
Tera Patron

@adityahubli 

Objects/arrays must be stringified (using JSON.stringify()) before sending from ServiceNow AJAX Script Include, because GlideAjax transmits only text, not native objects. On the client, use JSON.parse() to restore them.

This is required due to HTTP/AJAX using only text-based data formats, and GlideAjax’s string-only returns.

good explanation here

Ajax and JSON 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader