Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return value when AJAX request is succeeded [duplicate]

   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?

like image 839
Artur Keyan Avatar asked Jul 28 '11 05:07

Artur Keyan


People also ask

How do I return data after ajax call success?

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); });

Can we return a value from ajax call?

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.

How do I stop ajax requests duplicates?

click(function() { if(! requestSent) { requestSent = true; jQuery. ajax({ url: "http://example.com", ...., timeout: timeoutValue, complete: function() { ... requestSent = false; }, }); } });

How do I return a response from ajax?

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.


1 Answers

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;
}
like image 52
Pavel Surmenok Avatar answered Oct 04 '22 11:10

Pavel Surmenok