supa
supa

Reputation: 11

can i make a jquery ajax request in an ajax response?

I would like to make few ajax calls based on the response of my ajax call

something like this

$.post('test1.php', function(res) {

    var processid = res.processid;

    $.post('test2.php', id : processid, function (data){

        // do some stuff and make other ajax calls
    });

},'json')

Is this correct? i need to make additional requests based on the responses each request.

Thanks

Upvotes: 1

Views: 162

Answers (1)

gen_Eric
gen_Eric

Reputation: 227280

Yes, that is correct. The 2nd POST will run after the 1st completes.

Or, you can make a queue, using jQuery's $.queue and $.dequeue, and using $.data to store the variable. You could also use an AJAX Queue plugin (Google for them), I don't use any, so I can't suggest one.

//Add POST 1
$(document).queue('AJAX', function(next){
    $.post('test1.php', function(res){
        $(document).data('processid', res.processid); //We need to store this somewhere
        next(); //Call next queued function (if any)
    }, 'json');
});

//Add POST 2
$(document).queue('AJAX', function(next){
  $.post('test2.php', 'id='+$(document).data('processid'), function (data){
    next(); //Call next queued function (if any)
  });
});

// Start the queue
$(document).dequeue('AJAX');

Upvotes: 1

Related Questions