开发者

Jquery: send POST to multiple pages

开发者 https://www.devze.com 2023-03-29 23:36 出处:网络
I use the following script to force my server to recache a page, however I want to modify it so it can POST to many pages but use the same success function.

I use the following script to force my server to recache a page, however I want to modify it so it can POST to many pages but use the same success function.

function recache()
  {
$.ajax({
  type: 'POST',
  url: '../units_and_evidence/',
  data: 'cache=yes',
  success: alert("Reset cache successful")
  });
}

How would I provide multiple urls to POST t开发者_StackOverflowo using the same success function?


Got you, you want jQuery Deferreds.

Basically, the $.AJAX functions in jQuery 1.5 and above return a deferred object.

 var request1 = $.ajax({...});
 var request2 = $.ajax({...});

 $.when(request1, request2).then(function() {
      // Do something
 });

What happens is once both requests (or all items in the .when() method parameters) are resolved (handled with AJAX automatically for you), the function in the .then() method is executed. There's more, which you can find in online tutorials or the API docs.


Try this now:

// initialize here
var requestCallback = new MyRequestsCompleted({
    numRequest: 3,
});



var urls = ["http://www.test.com/users/", "http://www.example.com/users/", "http://www.test.org/users/"]

    $.each(urls, function(index, value) {
       $.ajax({
          type: "POST",
          data: 'cache=yes',
          url: value + get_back + "",
          success: requestCallback.requestComplete(true)
       });
    });



var MyRequestsCompleted = (function() {
    var numRequestToComplete, requestsCompleted, callBacks, singleCallBack;

    return function(options) {
        if (!options) options = {};

        numRequestToComplete = options.numRequest || 0;
        requestsCompleted = options.requestsCompleted || 0;
        callBacks = [];
        var fireCallbacks = function() {
            alert("Reset cache successful");
            for (var i = 0; i < callBacks.length; i++) callBacks[i]();
        };
        if (options.singleCallback) callBacks.push(options.singleCallback);

        this.addCallbackToQueue = function(isComplete, callback) {
            if (isComplete) requestsCompleted++;
            if (callback) callBacks.push(callback);
            if (requestsCompleted == numRequestToComplete) fireCallbacks();
        };
        this.requestComplete = function(isComplete) {
            if (isComplete) requestsCompleted++;
            if (requestsCompleted == numRequestToComplete) fireCallbacks();
        };
        this.setCallback = function(callback) {
            callBacks.push(callBack);
        };
    };
});

here we are listing all the URLs you want to POST to in a JavaScript Array and then using jQuery's $.each to loop through all urls and do an AJAX POST to each one.

Hope this helps.


Not sure this is what you had in mind, but you could pass a collection of strings to the function where each string is the url of the page you want to call.

function recache(pages)
{
     $(pages).each(function(){
       $.ajax({
        type: 'POST',
        url: this,
        data: 'cache=yes',
        success: alert("Reset cache successful")
        });
      });
}


here is 1 way to do this.. you need to fire multiple ajax requests to your server that is for sure but you can add an ajaxComplete listener in which you basically count the number of ajax requests that have finished successfully and when that number matches the total number of requests perform your necessary operations.


This is a simple example.Using switch case.

ajaxSuccess: function(data) {
                        switch (SetUrls.ajaxCallMode) {
                            case 0:
                                "Ur Url"
                                break;
                            case 1:
                              "Ur url"

                                break;
                            case 2:
                               "ur url"
                                break;
                        }
                    },


function recache(pages)
{
    var numComplete = 0;
    $(pages).each(function(){
        $.ajax({
        type: 'POST',
        url: this,
        data: 'cache=yes',
        success: function () {
            numComplete++;
            if (numComplete == pages.length) {
                alert("Success");
            }
        }
        });
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消