2

The situation is I am using an API to fetch some data and update my database. I want to show user about the update.

So my ajax request is something like this.

$.ajax({
    url:  '<?php echo base_url() ?>add_products/',
    data: "store_id="+store_id,
    type: 'POST',
    dataType: 'json',
    async : true,
    success: function(response) {
        if ( response.result == 'success') {
           //some data
        } else {
            genError();
        }
    }
});

and I am trying to get the process update like this.

    $.ajax({
        url:  '<?php echo base_url() ?>get_product_progress/',
        data: "store_id="+store_id,
        type: 'POST',
        dataType: 'json',
        async : true,
        success: function(response) {
            if ( response.result == 'success') {
                console.log(response);               
            } else {
                genError();
            }
        }
    });

I am not using any php session. I pass store id which fetch values from DB. I want to send 1 request that add products and other one to check how many products are added.

The problem is one 1st request of adding product is made, the get progress call does not progress. Get progress call is only made after the add product request is completed. I want them to be parallel.

I have found out that its server problem. Server blocks the second request until first request is completed. So how can I make the server return an ajax request when it is complete and make independent of each other.

Junaid Atique
  • 485
  • 1
  • 5
  • 19

1 Answers1

0

You can use $.when.

Here is a good blog post about it.
Docs.

Example:

$.when(
    $.ajax({
      url:  '<?php echo base_url() ?>add_products/',
     data: "store_id="+store_id,
     type: 'POST',
    dataType: 'json',
    async : true,
    success: function(response) {
        if ( response.result == 'success') {
           //some data
        } else {
            genError();
        }
      }
  });
).then(function() {
    $.ajax({
        url:  '<?php echo base_url() ?>get_product_progress/',
        data: "store_id="+store_id,
        type: 'POST',
        dataType: 'json',
        async : true,
        success: function(response) {
            if ( response.result == 'success') {
                console.log(response);               
            } else {
                genError();
            }
        }
   });
});
LifeQuery
  • 3,202
  • 1
  • 26
  • 35
  • I have tried $.when. it is used when you have different calls but one call back. my situation is that there is a single call with and there is not a single result. both have different results. when is not valid for this. – Junaid Atique May 28 '14 at 10:06
  • @JunaidAtique I'm not really sure what you're trying to achieve that you can do with `$.when`, have you tried `deferred.done()`? Or `.ajaxComplete()`? – LifeQuery May 28 '14 at 10:44
  • ok. I got the answer. the problem was with php session. I have initialized it global. but when I removed it from global and put it separate for each controller it worked. thanks for your time. – Junaid Atique May 28 '14 at 10:51