0

i run the ajax request with async set to false inside the loop, and it stopped when the counter at 2. here is the script

var x = 0;
for(i=0; i<10; i++){
    $.ajax({
          async: false,
          global: isGlobal,
          type: 'POST',
          url: url,
          dataType: 'json',
          data: JSON.stringify(body),
          headers: {'Content-type':'application/json', 'Accept':'application/json'},
          success: function(result){
            x = result.something.value;
          },
          error: function(){
              callback(false);
          }
    });
    console.log(x); // debug x value
}

any idea why this is not working correctly? PS: the url is cross domain

Dariel Pratama
  • 1,607
  • 3
  • 18
  • 49
  • you're not really doing ajax anymore? what is that mean? please comeup with code not just speak, i am lost! – Dariel Pratama Jun 09 '14 at 02:53
  • what i need to do is make the `x` variable changed for each request, that's it and if its wrong with my code please point me to the correct direction. thanks – Dariel Pratama Jun 09 '14 at 02:55
  • Ajax stands for *Asynchronous Javascript And Xml*, and you've just removed the *Asynchronous* part by setting async to false, so it's not really ajax it's sjax, *Synchronous Javascript And Xml*, and what's cool about that ? – adeneo Jun 09 '14 at 02:55
  • Open your console and see there. Maybe there is some error since it is cross orgin call and publish it here if there is one. – Khamidulla Jun 09 '14 at 02:58
  • hmm..i completely lost. the original script is not like that, instead it is fetch another data first, inside that data there is list of item and then inside that list i have and ID, with that ID i tried to call another WS to get another properties/value that is not inside the first data. – Dariel Pratama Jun 09 '14 at 02:58
  • @DarielPratama Regarding recursive function calls, see [Sending one AJAX request at a time from a loop](http://stackoverflow.com/questions/5066002/sending-one-ajax-request-at-a-time-from-a-loop). – Jonathan Lonowski Jun 09 '14 at 03:02
  • @JonathanLonowski thanks i'll take a look at it, that's better then just a clue :) – Dariel Pratama Jun 09 '14 at 03:03
  • here is screenshot from the console http://i.imgur.com/ViXutdR.png – Dariel Pratama Jun 09 '14 at 03:06
  • @DarielPratama In that case, all 10 rounds are succeeding. Notice the `9` in the circle on the left? That means there were 9 consecutive logs of the same value. The Console just condensed them rather than show them all individually. But, they're all accounted for (9 + the 1 before them). – Jonathan Lonowski Jun 09 '14 at 03:08
  • oh yes, my mistake. but when i debug the true data with `console.log` the value isn't 3 for all response, so what is wrong? – Dariel Pratama Jun 09 '14 at 03:09
  • Console would NOT group them and say they are `9` if they did not have the same value: `3`. So what, EXACTLY, do you mean? – PeterKA Jun 09 '14 at 03:29

1 Answers1

0

i solve this problem my self. here is the working script, in case you, visitor, have the same problem as mine.

var theList = [/*LIST GOES HERE*/];
var yourGlobalVar = '';
i = 0;
(function gcr(){
  var body = {
    ID: theList[i].ID
  };

  $.ajax({
          async: false,
          global: isGlobal,
          type: 'POST',
          url: url,
          dataType: 'json',
          data: JSON.stringify(body),
          headers: {'Content-type':'application/json', 'Accept':'application/json'},
          success: function(result){
            yourGlobalVar += result.anything.value;
            if(i < theList.length - 1){
              // wait for the response then call itself
              i++;
              gcr();
            }
          }
    });
})();
Dariel Pratama
  • 1,607
  • 3
  • 18
  • 49