
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 10:00 AM
Hello guys,
I need to return a script include in a client script. I'm using GlideAjax to do it.
This is the answer that is returning to the client script:
[{"name":"value1",
"member":"111111111", // sys id value
"area":"111111111", // sys id value
},]
Return of Glide Ajax in include script:
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.stringify(answer);
var obj = JSON.parse(answer);
alert('obj: ' + obj.area);
}
What I'm not able to do is get the values of the fields inside the array. It's returning undefined. How can I resolve this?
Thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 10:26 AM
Hello,
The example text is not in the correct format to convert to JSON so assuming it was simply a typo using the following example this is how you'd convert to object and get the value:
Example Array -
[{"name":"value1",
"member":"111111111",
"area":"111111111"
}]
Use the following code:
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var obj= JSON.parse(answer);
for(var i in obj){
alert('obj: ' + obj[i].area);
}
}
If there will only be one item in your returned array then the follow code is cleaner:
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var obj= JSON.parse(answer);
alert('obj: ' + obj[0].area);
}
Hope this helps.
--David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2022 05:54 AM
Hi David,
I have a similar issue whereby, in the script include, I look up a table that has a column for holding a set of name value pairs (it's defined as a 'Name-Value Pairs' type field).
I'd like to return this field's name/value pairs to the calling client script and loop through each entry and be able to reference both the name and the value to build a Json Payload.
At the moment I'm separating the names and values into 2 separate arrays and processing the arrays in the client script but wanted to check if you knew of a more efficient way to do this,
many thanks in advance,
Keiron.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2022 02:02 PM
Hello,
I'm not sure why you are separating out the names/values. When you query the column in the AJAX call you can return the stringified JSON object. Then in your client script you can parse the JSON and loop through as desired like so:
for (var key in obj) {
gs.print(key + ':' + obj[key]);
}
Hope this helps.
--David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2022 12:24 PM
Hi
1. There is a syntax error in the array, but i think its a typo.
2. Try returning a stringified JSON from the backend script include and parse it to JSON in client script.
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
var name = answer[0].name;
var member = answer[0].member;
var area = answer[0].area;
alert('name: ' + name+"\nmember: "+member+"\narea: "+area);
}
Thanks