I'm having a problem making ajax fast and functional. Here's the pseudo/prototype code:
function blah1(arg1){//arg1 is an array, roughly 10 elements
var arr[];
$.each(arg1, function(i){
//blah code
$.ajax({
//blah options
async: true,
success: function(data){
arr[i] = data.someInt;
}//end success
});//end ajax
}//end each
return arr;
}//end function
Basically, I'm sending an ajax and need the returned data for further processing.
If I set async to true, the function immediately returns empty 'arr' array, thus the whole script fails. But if I set async to false, then it works, but takes very long.
I have seen this $.ajaxQueue(); thing, but frankly I don't understand it at all, and I don't know if it will work.
So the question is, firstly, is there any way I can asynchronously send all the ajax requests at the same time and let function wait and return arr[] after all ajax are done? If not, will the ajaxQueue work in my case? (rough example please?)
If you don't want the $. ajax() function to return immediately, set the async option to false : $(". my_link").
success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.
Yes, it is deprecated in jQuery 1.8 onwards. You should use . done() and use . fail() to catch the errors.
AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.
Using jQuery 1.5 deferred's I would opt for this :
function blah1(arr, callback){
$.when($.map(arr, function(val, i){
$.ajax({
//blah options
async: true
});
}).toArray()).then(function(resultsArr) {
callback(resultsArr);
});
}
The problem was you were trying to return the array in your function before the async ajax calls finish. This isn't really possible so you will need to pass a callback to blah.
What your doing here is mapping your array of objects to jqXHR objects (which are deferred objects). Then passing that array of deferred objects to $.when
.
$.when
takes an array and then allows you to run the .then
function when the entire array has finished loading from the ajax calls. You then have a resultsArr
passed in as an argument to your .then
function.
There is no way to use $.ajax
and return
in the same function if you manipulate the return value in your ajax success call.
You could make the Ajax call synchronous which you seem to know about, personally I would re factor my code so the success method of the ajax call, then triggers a call off to another function.
$.ajax({
//blah options
async: true,
success: function(data){
arr[i] = data.someInt;
myCall(arr[i]);
}//end success
});//end ajax
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