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`.
XMLHttpRequest 2 (first of all read the answers from Benjamin Gruenbaum & Felix Kling)
If you don't use jQuery and want a nice short XMLHttpRequest 2 which works on the modern browsers and also on the mobile browsers I suggest to use it this way:
As you can see:
There are two ways to get the response of this Ajax call (three using the XMLHttpRequest var name):
The simplest:
Or if for some reason you
bind()
the callback to a class:Example:
Or (the above one is better anonymous functions are always a problem):
Nothing easier.
Now some people will probably say that it's better to use onreadystatechange or the even the XMLHttpRequest variable name. That's wrong.
Check out XMLHttpRequest advanced features
It supported all *modern browsers. And I can confirm as I'm using this approach since XMLHttpRequest 2 exists. I never had any type of problem on all browsers I use.
onreadystatechange is only useful if you want to get the headers on state 2.
Using the
XMLHttpRequest
variable name is another big error as you need to execute the callback inside the onload/oreadystatechange closures else you lost it.Now if you want something more complex using post and FormData you can easily extend this function:
Again ... it's a very short function, but it does get & post.
Examples of usage:
Or pass a full form element (
document.getElementsByTagName('form')[0]
):Or set some custom values:
As you can see I didn't implement sync... it's a bad thing.
Having said that ... why don't do it the easy way?
As mentioned in the comment the use of error && synchronous does completely break the point of the answer. Which is a nice short way to use Ajax in the proper way?
Error handler
In the above script, you have an error handler which is statically defined so it does not compromise the function. The error handler can be used for other functions too.
But to really get out an error the only way is to write a wrong URL in which case every browser throws an error.
Error handlers are maybe useful if you set custom headers, set the responseType to blob array buffer or whatever...
Even if you pass 'POSTAPAPAP' as the method it won't throw an error.
Even if you pass 'fdggdgilfdghfldj' as formdata it won't throw an error.
In the first case the error is inside the
displayAjax()
underthis.statusText
asMethod not Allowed
.In the second case, it simply works. You have to check at the server side if you passed the right post data.
cross-domain not allowed throws error automatically.
In the error response, there are no error codes.
There is only the
this.type
which is set to error.Why add an error handler if you totally have no control over errors? Most of the errors are returned inside this in the callback function
displayAjax()
.So: No need for error checks if you're able to copy and paste the URL properly. ;)
PS: As the first test I wrote x('x', displayAjax)..., and it totally got a response...??? So I checked the folder where the HTML is located, and there was a file called 'x.xml'. So even if you forget the extension of your file XMLHttpRequest 2 WILL FIND IT. I LOL'd
Read a file synchronous
Don't do that.
If you want to block the browser for a while load a nice big
.txt
file synchronous.Now you can do
There is no other way to do this in a non-asynchronous way. (Yeah, with setTimeout loop... but seriously?)
Another point is... if you work with APIs or just your own list's files or whatever you always use different functions for each request...
Only if you have a page where you load always the same XML/JSON or whatever you need only one function. In that case, modify a little the Ajax function and replace b with your special function.
The functions above are for basic use.
If you want to EXTEND the function...
Yes, you can.
I'm using a lot of APIs and one of the first functions I integrate into every HTML page is the first Ajax function in this answer, with GET only...
But you can do a lot of stuff with XMLHttpRequest 2:
I made a download manager (using ranges on both sides with resume, filereader, filesystem), various image resizers converters using canvas, populate web SQL databases with base64images and much more... But in these cases you should create a function only for that purpose... sometimes you need a blob, array buffers, you can set headers, override mimetype and there is a lot more...
But the question here is how to return an Ajax response... (I added an easy way.)
You can use this custom library (written using Promise) to make a remote call.
Simple usage example:
Short answer: Your
foo()
method returns immediately, while the$ajax()
call executes asynchronously after the function returns. The problem is then how or where to store the results retrieved by the async call once it returns.Several solutions have been given in this thread. Perhaps the easiest way is to pass an object to the
foo()
method, and to store the results in a member of that object after the async call completes.Note that the call to
foo()
will still return nothing useful. However, the result of the async call will now be stored inresult.response
.Using Promise
The most perfect answer to this question is using
Promise
.Usage
But wait...!
There is a problem with using promises!
Why should we use our own custom Promise?
I was using this solution for a while until I figured out there is an error in old browsers:
So i decided to implement my own Promise class for ES3 to below js compilers if its not defined. Just add this code before your main code and then safely use Promise!
While promises and callbacks work fine in many situations, it is a pain in the rear to express something like:
You'd end up going through
async1
; check ifname
is undefined or not and call the callback accordingly.While it is okay in small examples it gets annoying when you have a lot of similar cases and error handling involved.
Fibers
helps in solving the issue.You can checkout the project here.
You are using Ajax incorrectly. The idea is not to have it return anything, but instead hand off the data to something called a callback function, which handles the data.
That is:
Returning anything in the submit handler will not do anything. You must instead either hand off the data, or do what you want with it directly inside the success function.