0

based on below code, i want to understand why i cannot call data from json (json.projects[i].projName) when i am calling it from ajax function?

var json = JSON.parse(data);

for (var i = 0; i < json.projects.length; i++) {  

alert(json.projects[i].projName); //will show correct data

    $.ajax({ url: epridlist, method: 'GET'}).then(function (datas) {
        alert(json.projects[i].projName) // this will fail. says not know projName, when projName existed in jason data
    });
}

Please help me understand. if i alert outside ajax function, result will show

Kevin B
  • 94,570
  • 16
  • 163
  • 180
silentHijab
  • 63
  • 1
  • 8
  • I rolled back your question because your edit invalidated existing answers. If the answer answered your question, accept it. If you have followup questions, please ask a new question. – Kevin B Jan 24 '17 at 20:53
  • I did read the post, and I cannot relate it with my code. my data is from json array and the example just count i. whats the relation? this is so frustrating – silentHijab Jan 24 '17 at 20:55
  • in your edit... you said your problem was solved, and you had a new problem after adding a new inner loop, that's different. From what i saw of it before rolling back, it was completely unrelated to your original problem because you were properly using `let` to avoid the closure loop problem. [The new problem](http://stackoverflow.com/posts/41835949/revisions) was likely a case of passing incorrect data. – Kevin B Jan 24 '17 at 20:56
  • it is correct data. and this isnt duplicated question. – silentHijab Jan 24 '17 at 21:00
  • Your original question is in fact a duplicate. Your new question is not, however, your new question invalidates your existing answers, and therefore cannot be edited into your original question. – Kevin B Jan 24 '17 at 21:11
  • Here is a jsfiddle re-creating your "new" question with invalid object literal fixed. https://jsfiddle.net/kwmk52ca/1/ – Kevin B Jan 24 '17 at 21:11
  • And, of course, the problem is because one of your... tasks don't have a taskName, instead it has a projName. – Kevin B Jan 24 '17 at 21:13
  • my task have taskName, and still your code not run good. your code in fiddle, i tested with my json file, still it gave me undefined value. i have ajax function between each for loop, and this increase my problems. Btw thanks for your kindness to help. I already submit new post on this. – silentHijab Jan 24 '17 at 21:45

1 Answers1

1

This is because of closure. The inner function in the Ajax success callback creates a closure and value of variable i is captured. The inner function contains a reference to the i variable.

After the loop executes, The value of i would be json.projects.length and when you try to access the json.projects[json.projects.length] you would get undefined.

To solve your problem you can try using the let variable instead of var which has a shorter scope as compared to the function scope.

You can refer to other approaches in this question: JavaScript closure inside loops – simple practical example

var json = JSON.parse(data);

for (let i = 0; i < json.projects.length; i++) {  

alert(json.projects[i].projName); //will show correct data

    $.ajax({ url: epridlist, method: 'GET'}).then(function (datas) {
        alert(json.projects[i].projName) // this will fail. says not know projName, when projName existed in jason data
    });
}
Community
  • 1
  • 1
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41