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

Thanks, this is solution i looking for.