0

I have next code and as I know this is not so good,

 var addrIds = [1000,1001,1003,1004,1005];


 $.each(addrIds, function (index, adr) {
                                var data = { "addressId": parnetId, "parentId": adr };
                                $.ajax({
                                    url: '/Change',
                                    type: 'POST',
                                    contentType: 'application/json',
                                    dataType: "json",
                                    cache: false,
                                    data: JSON.stringify(data),
                                    success: function (result) {

                                    },
                                    complete: function (e, xhr, settings) {
                                        switch (e.status) {
                                            case 200:
                                                break;
                                            default:
                                                {
                                                    console.log(e.statusText);                                              
                                                }
                                        }
                                    }
                                }); 
                        });

is it possible to re factor this and create chain of ajax requests

Arbejdsglæde
  • 13,670
  • 26
  • 78
  • 144
  • Just send your entire data to the server trough a single request and let the server do the "change" for-each one of them. Or that is not feasible with your current environment? If it's so, then what do you understand trough chaining ajax request, cause sending them async is the way you want them, as you are doing now. If you are thinking of waiting for each request to send another one is just ... not right! – Leth Aug 14 '13 at 11:00
  • if it not possible to do in one request ? – Arbejdsglæde Aug 14 '13 at 11:08
  • If it's not possible i would go with sending requests the way you do, but we have a problem if we want to do something with the response from each request as a whole. Then we need to implement a mechanism trough which we are notified when the last request was done, and i am not saying that the last request is the one you fire last, it's the one that is completed the last. Also take in consideration that every browser has a maximum concurrent Ajax request to a domain, and vary from 2 in IE7 and 6 in Chrome. – Leth Aug 14 '13 at 11:25
  • Anyway you can check this [link](http://stackoverflow.com/questions/5066002/sending-one-ajax-request-at-a-time-from-a-loop/5066125#5066125) maybe it helps. – Leth Aug 14 '13 at 11:29

1 Answers1

0

Why don't you make 1 ajax call with a separated string like that :

1001;1002;1003

And in your server method you just call an explode method to do stuff on all your addrIds

jrk
  • 43
  • 1
  • 8