How can I find out what parameters were passed to an (asynchronous) jQuery AJAX request after it is complete?
Basically, I am firing a bunch of AJAX requests with different id passed as a parameter and displaying the results to the user when each of them finishes. Now, if the request is successful, I can pass the id back in the result of the request. But, if the request fails, I don’t know which request failed, i.e. for what id was that request.
Example (javascript/jQuery):
for(var i=0; i < 10; i++ ) {
$.ajax({
type: "GET",
url: "get-results.php",
data: { 'id': i },
success: function(data, textStatus, xmlReq) {
data = $.parseJSON(data);
$("#result" + data.id).html(data.result);
},
error: function(xmlReq, textStatus, errorThrown) {
var id= ???;
$("#result" + id).html('error bla bla');
}
});
}
You need to capture the current value of
ifor theerrorfunction. And you capture with a closure. So:That is
erroris the result of immediately calling an anonymous function passing in the current value ofi.