简体   繁体   中英

How to sent data by ajax in multiple urls

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();
});

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
     });
  });
)};

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM