GlideAjax Question - how to use response in other places in my code?

kemmy1
Tera Guru

I need to get the answer from my GlideAjax script and use this answer in other if statements in my client script.  How do I do this?

 

Simple GlideAjax:

var getStudent = new GlideAjax('<glideAjax');
getStudent.addParam('sysparm_name', 'isLawStudent'); 
getStudent.getXMLAnswer(getStudentResult);
 
function getStudentResult(response) {
   g_form.addInfoMessage("response: " + response);
   var student2 = response;
}
    g_form.addInfoMessage("student2: " + student2); //not getting data
 
Thanks in advance?
1 ACCEPTED SOLUTION

Hi @kemmy1 ,

 

Why don't u define your logic in your existing function where u receiving response from GlideAjax? Something like below

 var ga = new GlideAjax('asu_GetLocationData');
    ga.addParam('sysparm_name', 'getCampus');
    ga.addParam('sysparm_buildingid', g_form.getValue("u_building"));
    ga.getXMLAnswer(updateCampus);
}

function updateCampus(answer) {
    var clearvalue; // Stays Undefined
    if (answer) {
         var returneddata = JSON.parse(answer);
         g_form.setValue("campus", returneddata.sys_id, returneddata.name);
    } else {
         g_form.setValue("campus", clearvalue);
    }
}

Thanks,

Danish

 

View solution in original post

13 REPLIES 13

Bert_c1
Kilo Patron

Hi,

 

I'm guessing you see the "response:" message, and not the :student2:" message. If so, that would be because you declare 'student2' in the function, and it is not valid outside of the function.  What do you want to do with the 'response' there?

I want to take the response (it's a yes or no) and use it in other If statements in my client script.

Vishal Birajdar
Giga Sage

Hi @kemmy1 

You can try declaring variable outside and then assign value

var student2;
var getStudent = new GlideAjax('<glideAjax');
getStudent.addParam('sysparm_name', 'isLawStudent'); 
getStudent.getXMLAnswer(getStudentResult);
 
function getStudentResult(response) {
   g_form.addInfoMessage("response: " + response);
   student2 = response;
}
g_form.addInfoMessage("student2: " + student2);

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

That's what I tried as well, still not getting any data back:
var getStudent = new GlideAjax('<glideAjax');
getStudent.addParam('sysparm_name', 'isLawStudent'); 
getStudent.getXMLAnswer(getStudentResult);
var student2 = '';
 
function getStudentResult(response) {
   g_form.addInfoMessage("response: " + response);
   student2 = response;
}
    g_form.addInfoMessage("student2: " + student2); //not getting data