How to return GlideAjax value

Muhammad Arif B
Tera Contributor

Hi, I have UI Action where need to display alert of string respStr. But the problem cant get the value from mycallback function, Here my script :

 

 

function closeComplete(){
         alert(checkFile());
}


function checkFile() {
        sys_id = g_form.getUniqueValue();
	
	var ga = new GlideAjax('AjaxFSMClosureValidation');
	ga.addParam('sysparm_name', 'callFuntionFileValidate');
	ga.addParam('sys_id', sys_id);
	ga.getXML(mycallback);

	function mycallback(response) {
		var respStr = response.responseXML.documentElement.getAttribute('answer');
		return respStr;
	}
	return mycallback;
}

 

 

Is there any way to get the value of respStr and return to function closeComplete?

 

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Muhammad Arif B 

It looks like you're trying to get the value of respStr from the mycallback function and use it in the closeComplete function. However, due to the asynchronous nature of AJAX requests, you can't directly return a value from mycallback to closeComplete.

Instead, you can modify your code to pass a callback function to checkFile and execute that callback when the response is received.

 

function closeComplete(){
    checkFile(function(respStr) {
        alert(respStr);
    });
}

function checkFile(callback) {
    var sys_id = g_form.getUniqueValue();

    var ga = new GlideAjax('AjaxFSMClosureValidation');
    ga.addParam('sysparm_name', 'callFuntionFileValidate');
    ga.addParam('sys_id', sys_id);
    ga.getXML(function(response) {
        var respStr = response.responseXML.documentElement.getAttribute('answer');
        callback(respStr);
    });
}

 

Now, checkFile accepts a callback function as an argument and executes it with respStr as its parameter once the response is received. In closeComplete, you pass an anonymous function to checkFile, which displays the alert with the received respStr.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

Without insights into your complete implementation and thrown error messages it is difficult to answer but I saw at least one error in your script. Try the following version:

function closeComplete(){
   checkFile();
}


function checkFile() {
        var sys_id = g_form.getUniqueValue();
	
	var ga = new GlideAjax('AjaxFSMClosureValidation');
	ga.addParam('sysparm_name', 'callFuntionFileValidate');
	ga.addParam('sys_id', sys_id);
	ga.getXML(mycallback);

	function mycallback(response) {
		var respStr = response.responseXML.documentElement.getAttribute('answer');

		alert(respStr);
	}
}

 

 

James Chun
Kilo Patron

Hi @Muhammad Arif B,

 

I don't believe there is a way to return the value of respStr to closeComplete() as the callback function is executed asynchronously.

You will have to add the alert within the callback function.

 

Cheers

SK Chand Basha
Giga Sage

Hi @Muhammad Arif B 

 

function closeComplete(){
checkFile();
}


function checkFile() {
var sys_id = g_form.getUniqueValue();

var ga = new GlideAjax('AjaxFSMClosureValidation');
ga.addParam('sysparm_name', 'callFuntionFileValidate');
ga.addParam('sys_id', sys_id);
ga.getXMLAnswer(mycallback); 

function mycallback(answer) { 
alert(answer); 
}
}

Mark it Helpful and Accept Solution!! If this helps you to understand.

Maddysunil
Kilo Sage

@Muhammad Arif B 

It looks like you're trying to get the value of respStr from the mycallback function and use it in the closeComplete function. However, due to the asynchronous nature of AJAX requests, you can't directly return a value from mycallback to closeComplete.

Instead, you can modify your code to pass a callback function to checkFile and execute that callback when the response is received.

 

function closeComplete(){
    checkFile(function(respStr) {
        alert(respStr);
    });
}

function checkFile(callback) {
    var sys_id = g_form.getUniqueValue();

    var ga = new GlideAjax('AjaxFSMClosureValidation');
    ga.addParam('sysparm_name', 'callFuntionFileValidate');
    ga.addParam('sys_id', sys_id);
    ga.getXML(function(response) {
        var respStr = response.responseXML.documentElement.getAttribute('answer');
        callback(respStr);
    });
}

 

Now, checkFile accepts a callback function as an argument and executes it with respStr as its parameter once the response is received. In closeComplete, you pass an anonymous function to checkFile, which displays the alert with the received respStr.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks