Passing variables to $.ajax().done() Passing variables to $.ajax().done() ajax ajax

Passing variables to $.ajax().done()


You can just create a custom field in the object that you send to $.ajax(), and it will be a field in this when the promise callback is made.

For example:

$.ajax( { url: "https://localhost/whatever.php", method: "POST", data: JSON.stringify( object ), custom: i // creating a custom field named "custom" } ).done( function(data, textStatus, jqXHR) { var index = this.custom; } );


You can use a closure (via a self executing function) to capture the value of i for each invocation of the loop like this:

for (var i in obj) {    (function(index) {        // you can use the variable "index" here instead of i        $.ajax(/script/).done(function(data){ console.log(data); });    })(i);}