I am trying to make some requests to an url and dependening on the length of the data that I get as a response to make a for loop length times, this is an example:
<script>
var auxData;
var message;
$.ajax({
url: "some url",
type: "GET",
dataType: "json",
success: function (result) {
auxData = eval(result["Some key"])
for(var i = 0, l = (auxData.length-1); i < l; ++i)
{
console.log(i)
$.ajax({
url: "another url",
type: "GET",
dataType: "json",
success: function (result) {
message = eval(resul["Some key"])
console.log("Value of" + i) //ALWAYS PRINTS 8
message= message[i] //RETURNS ERROR
}.bind(this)
});
}
}
});
</script>
I can't put all code here since its so long and confusing, but what happens is if the length of the variable auxData
is 9, the line console.log(i)
prints in console:
0
1
2
3
4
5
6
7
8
Then After all this was printed the line console.log("Value of" + i)
prints this:
8
8
8
8
8
8
8
8
8
And finally if I try to access i
like message= message[i]
it doesn't work, It's like the calls inside the for loop are made separately. I have searched about what could be the problem but the only solutions were making the variable i
global using callbacks but I don't know how to do it, also found this answer here in stackoverflow about Accessing javascript variable inside ajax success but it didn't work for me, any solution would be really appreciated.