0

Is it possible to loop an ajax url like the mentioned code below?

var partsOfStr = [james, mary, kingkong]
 for (i = 0; i < partsOfStr.length; i++) {

  $.ajax({
  url: **'http://mp02.mybitmp.org/friendzone/getsurvey.php?Name=' + 
  partsOfStr[i],**
  dataType: 'json',
    success: function (done) {
     console.log(done);
     alert(partsOfStr[i])
    }
  });
}
Lakindu Gunasekara
  • 4,221
  • 4
  • 27
  • 41
Yi Kiat
  • 441
  • 1
  • 4
  • 6

3 Answers3

0

As long as you do not expect your answer results (i.e. the success callback) to come back at the same time, then yes. Otherwise, you will see the logs for different values in random orders because making AJAX requests is Asynchronous.

If you want to do the loop so that the results come back in order, you have to use Promises or do some form of calling the next element in the success or failure callback. I'd suggest using Promise.all

Robert Vunabandi
  • 150
  • 2
  • 12
0

The only problem you have with this code is relating to alert(partsOfStr[i]).

It will always give the output 2 - because the variable i is being accessed in a closure. By the time it is accessed, the loop is over and the final value of i will be 2.

Look at this question for details.

Additionally, if you have to make these requests in the same order as specified in the array, then you will have to remove the loop and recurse the call from within the results.

Charlie
  • 22,886
  • 11
  • 59
  • 90
0

As Charlie mentioned when you use async in a loop or something with a callback you can't use the loop variable as a direct closure.

A simple solution is as Robert mentions to use Promise.all since $.ajax returns a promise like object. It would look something like this:

var partsOfStr = [james, mary, kingkong];

Promise.all(
  partsOfStr.map(
    function(part){
      return $.ajax({
        url: 'http://mp02.mybitmp.org/friendzone/getsurvey.php?Name=' + part,
        dataType: 'json',
      })
      .then(
        function(result){
          console.log("result for "+part+" received");
          return result;
        }
      )
    }
  )
).then(
  function(results){
    console.log("All results received",results.length);
  }
).catch(
  function(err){
    console.warn("Something went wrong:",err);
  }
);

If you want to support browsers that don't have native promises or don't want to polyfil it you can use jQuery.when instead of Promise.all:

var partsOfStr = [james, mary, kingkong];

$.when.apply(
  $,
  partsOfStr.map(
    function(part){
      return $.ajax({
        url: 'http://mp02.mybitmp.org/friendzone/getsurvey.php?Name=' + part,
        dataType: 'json',
      })
      .then(
        function(result){
          console.log("result for "+part+" received");
          return result;
        }
      )
    }
  )
).then(
  function(results){
    console.log("All results received",results.length);
  }
).catch(
  function(err){
    console.warn("Something went wrong:",err);
  }
);
HMR
  • 37,593
  • 24
  • 91
  • 160