Passing variables to $.ajax().done()
Solution 1
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);
}
Solution 2
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; } );
Phil Tune
(2015) I do front-end development for a small company in East TN. I'm a StackOverflow enthusiast and tend to click here before even MDN for most Google searches. I am fanatic about general performance and accessibility. My SO questions tend to be about experimental concepts. I like building my own tools (re-re-reinventing the wheel) for its own sake. I'm obsessed with producing elegant, readable code. I'm very OCD (though my mother has never had me tested) and proud of it; I will rewrite projects several times and feel like it was time well-spent as long as it produces even a marginally better product. I love reading about new technologies and W3C rec's, but as my company's stuck on supporting IE8 (and graceful degradation can be a pain) I've yet to experiment with a lot of the new browser implementations, let alone the experimental stuff. I love jQuery but find myself lately trying to use it less and less in favor of vanilla js. As for other libraries I tend to be very averse and prefer custom options, even if it's more work - learning is never a wasted effort. My wife and I - both transplants from southeast Missouri - live right next to the Smoky Mountains and regularly serve in ministries at our local church. We don't have cable or smartphone data plans but watch a lot of Netflix. We like all natural foods (though I'd never turn down a bag of Doritos) and love to visit farms and do other "free" activities.
Updated on June 12, 2022Comments
-
Phil Tune almost 2 years
I'm lost. How might I pass a loop variable to an AJAX .done() call?
for (var i in obj) { $.ajax(/script/).done(function(data){ console.log(data); }); }
Obviously, if I were to do
console.log(i+' '+data)
i would return the very last key in the objectobj
on every single iteration. Documentation fails me. -
Phil Tune about 12 yearsClosures are one concept I've always had a tough time understanding fully. Thanks @jfriend00, I'ma try that!
-
Phil Tune about 12 yearsYep, that worked. I could even do
obj[index]
. Thanks again! -
Phil Tune over 7 yearsAfter 5 years, I have long since forgotten what even prompted this question. ;)
-
Darwin Airola over 7 yearsI needed to know how to do this for a recent project. So, when I figured it out, I tried to also relay the information to others who said that they wanted it...
-
Deep Saurabh almost 7 yearsThis is better from jquery ajax asynchronous execution point of view, it clearly provide intended item/object reference when callback is executed latter in time.