function ajaxRefresh(actionUrl) {
$.ajax({
url: actionUrl,
success: function() {
return true;
}});
return false;
}
The function anyway returns false
even when a request is succeeded, becouse the request is asynchronous. How I can return true
when request is succeeded?
You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });
You can't return "true" until the ajax requests has not finished because it's asynchron as you mentioned. So the function is leaved before the ajax request has finished.
click(function() { if(! requestSent) { requestSent = true; jQuery. ajax({ url: "http://example.com", ...., timeout: timeoutValue, complete: function() { ... requestSent = false; }, }); } });
What you need to do is pass a callback function to the somefunction as a parameter. This function will be called when the process is done working (ie, onComplete): somefunction: function(callback){ var result = ""; myAjax = new Ajax.
You should set parameter async
to false
. Try this code:
function ajaxRefresh(actionUrl) {
var succeed = false;
$.ajax({
async: false,
url: actionUrl,
success: function() {
succeed = true;
}});
return succeed;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With