0

For my script I want to send few data in multiple url's by ajax for multiple php query. So I tried as below which not call ajax waitForRep(). How to do it please?

my Javascript:

var url = ['/server/server2','/server/server'];
var tutid = '<?php echo $tutid; ?>';
var CID = '<?php echo $id; ?>';

function waitForRep(){
    $.each(url, function(i,u){
        $.ajax({
            type: "GET",
            cache: false,
            data: {
            tutid : tutid,
            CID : CID
        },
            timeout:15000, 
            success: function(data){ 
                // do something with result
                setTimeout(waitForRep, 15000 );
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(waitForRep, 15000); 
            }
        });
    }
}

$(document).ready(function(){
    waitForRep();
});
StepUp
  • 36,391
  • 15
  • 88
  • 148
koc
  • 955
  • 8
  • 26

2 Answers2

0

Your problem is with setTimeOut. It looks like you're trying to call the ajax twice.

If you want to call the second ajax on success, there's no need to iterate through the array. Simply call another ajax on the first ajax success.

Get rid of $.each and do it like this:

$.ajax ({
  url: link1
  success: function (data){
    $.ajax ({
      url: link2
     });
  });
)};
JJJ
  • 3,314
  • 4
  • 29
  • 43
0

If you're going to send multiple requests to multiple URLs: just send each by the same request, params...

But if you want to handle responses after sending all requests, the first idea in my mind to solve this is Promise, I tried with native Promise and it worked, but if you prefer jQuery, I suggest you have a look on jQuery.when(), this one maybe what you're looking for ( check the deferred part).

Kai
  • 3,104
  • 2
  • 19
  • 30