Home How do I return the response from an asynchronous call?
Reply: 0

How do I return the response from an asynchronous call?

user690
1#
user690 Published in April 22, 2018, 10:07 pm

I have a function foo which makes an Ajax request. How can I return the response from foo?

I tried returning the value from the success callback as well as assigning the response to a local variable inside the function and returning that one, but none of those ways actually return the response.

function foo() {
    var result;

    $.ajax({
        url: '...',
        success: function(response) {
            result = response;
            // return response; // <- I tried that one as well
        }
    });

    return result;
}

var result = foo(); // It always ends up being `undefined`.
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.632666 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO