1

I'm trying to send a post request to multiple servers (possible tanks to CORS). To do that i have an array with the jQuery posts and register the same callback function for the individual done's like so:

var requestUrls = getRequestUrlArrayFromForm();
var companyNames = getCompanyNamesArrayFromForm();

cleanTable();
for(var i = 0; i < requestUrls.length; i++) {

  postings.push($.post(requestUrls[i],
    someXmlString                               
  ));

  postings[i].done(function( response , textStatus, jqXHR) {    

    console.log(i); // the number printed here is always the same even for requestUrls.length > 1
    addToTable(companyNames[i], response);      

  }).fail(function( jqXHR ) {
    // do something
  });

}

My problem now is that i want to remember some data associated to each individual post like in this example a companyName. But in the success function of the post i dont know which of thes n-post requests i am and so can not know the right index do access the correct value in my companyName array. And okay i guess its becaus of ajax's asynchronous behavior. But anyway this problem should somehow be solvable.

So is it possible to pass some data to the post object that is not send to the server but just remembered by the client and accessible in the done function-callback?

Thanks in advance.

3 Answers 3

1

The way you need to go is something like this

var requestUrls = getRequestUrlArrayFromForm();
var companyNames = getCompanyNamesArrayFromForm();

cleanTable();
var postings = [];
for (var i = 0; i < requestUrls.length; i++) {
    postings.push($.post(requestUrls[i], someXmlString));
}

$.when.apply($, postings).done(function () {
    for (var i in arguments) {
        addToTable(companyNames[i], arguments[i][0]); // the response
    }
});

As per OP comments, updated answer

var requestUrls = getRequestUrlArrayFromForm();
var companyNames = getCompanyNamesArrayFromForm();

cleanTable();
for (var i = 0; i < requestUrls.length; i++) {
    (function (i) {
        $.post(requestUrls[i], someXmlString).done(function (resp) {
            addToTable(companyNames[i], resp);
        });
    })(i);
}

you need to wrap it with closure.

sample working demo

3
  • so why do you use the function apply together with when?
    – tObi
    Jul 4, 2014 at 12:24
  • @tobi good question, $.When can take any number of arguments usually deferred objects. apply used here for a convenient to pass the parameters as an array and to pass the context. $.when.apply($, postings).done() literally means, when all these promises are resolved... do something.
    – code-jaff
    Jul 4, 2014 at 14:07
  • so if i understood it correctly with when i can do something if all posts where answered.. what i try is to fill the table step by step as each post response arrives :/
    – tObi
    Jul 5, 2014 at 19:37
0

How about something like this?

var requestUrls = getRequestUrlArrayFromForm();
var companyNames = getCompanyNamesArrayFromForm();

cleanTable();
for(var i = 0; i < requestUrls.length; i++) {

  $.post( requestUrls[i], someXmlString)
    .done(function(response , textStatus, jqXHR) {
        addToTable(companyNames[i], response);
    })
    .fail(function( jqXHR ) {
        // do something
    });

}
1
  • the code in done is still just executed after the for loop is done and i gets the value of requestUrls.length like in my example
    – tObi
    Jul 5, 2014 at 19:35
0

Maybe wrapper function will help:

var requestUrls = getRequestUrlArrayFromForm();
var companyNames = getCompanyNamesArrayFromForm();

cleanTable();
for(var i = 0; i < requestUrls.length; i++) {

    postings.push($.post(requestUrls[i],
        someXmlString                               
    ));

    postings[i].done((function(j){ 
        return function( response , textStatus, jqXHR ) {    
            console.log(j); 
            addToTable(companyNames[j], response);      
        }
    })(i)).fail(function( jqXHR ) {
        // do something
    });

}
1
  • no, this function is not called by jquerys post response
    – tObi
    Jul 5, 2014 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.