Martyna va
Martyna va

Reputation: 181

Send ajax request for multiple urls

I want to trigger 2 emails with different templates for different recipients. I tired to add another url next to url:, however it didn't work. Is there a way to add more that one emailer to ajax request? Your advice is much appreciated. Thanks

$.ajax({
    type: "POST",
    url: "../wp-admin/emailer.php","../wp-admin/emailer2.php" // add more emailers
    data: dataString,
    success: function(){ return true;}
});
window.location.href = "thank-you";

Upvotes: 3

Views: 3083

Answers (4)

Raj Nandan Sharma
Raj Nandan Sharma

Reputation: 3862

If you want to handle the callback for each email and given you will have less than 100 emails, you can use recursion Eg Show a thank you only when all the emails have been sent successfully

var urls=["url1","url2","url3","url4"];
var done=true;
function sendEmails(){

    if(urls.length==0 && done){
        alert("All emails sent successfully");
        return true;
    }else if(urls.length>0 && !done){
        alert("There was an error");
        return true;
    }
    else{
        var url=urls.pop();
        $.ajax({
            type: "POST",
            url: url,
            data: dataString,
            success: function(){ 
                sendEmails()
            },
            error:function(){
                done=false; 
                sendEmails();
            }
        });
    }
}
sendEmails();

Upvotes: 0

charlietfl
charlietfl

Reputation: 171679

Can use $.when() to fire after multiple requests succeed:

var req1 = $.post("../wp-admin/emailer.php", dataString);
var req2 = $.post("../wp-admin/emailer2.php", dataString);

$.when(req1,req2).then(function(){
     // all requests succeeded
     window.location.href = "thank-you";
}).fail(function(){
    // oops, something went wrong with at least one of the requests
})

Upvotes: 1

Chirag Suthar
Chirag Suthar

Reputation: 21

Use This Methods to Call Multiple URl withb Data.... It also help for future call or Dynamic call in any where in Your code

function sendMyAjax(URLAdd,dataString){
     $.ajax({
         type: "POST",
         url: URLAdd
         data: dataString,
         success: function(){ return true;}
      });
    window.location.href = "thank-you";
    }
};

sendMyAjax("../wp-admin/emailer.php","DataString");
sendMyAjax("../wp-admin/emailer.php","DataString");

You Can Use Array for the store URL and Data

Upvotes: -1

orvi
orvi

Reputation: 3340

You can't do that becuase a single AJAX request can only talk to one URL, but you can create multiple AJAX requests for different URLs, and they will do their work simultaneously - you wouldn't need to wait for one to complete before firing off the next one. So in that case you have to take an array where you reserve all the urls.Then you need is $.each and the two parameter form of $.ajax

var urls = ['/url/one','/url/two', ....];

$.each(urls, function(i,u){ 
     $.ajax(u, 
       { type: "POST",
         data: dataString;
         success: function(){
           return true;
         } 
       }
     );
});
window.location.href = "thank-you";
}

Upvotes: 1

Related Questions