Amitabh
Amitabh

Reputation: 739

Jquery $.post not working on chrome but works in IE and FF

The code snippet for the problem I am facing is:

//select the shipping country to display its shipping rate
  $('#country').change(function() {

     if($(this).val() == ""){
          $('#shippingRateDisplay').html('<br /><span style="margin-left:8px">No country selected.</span>');
          return false;
        }
        alert("Before ajax start");
    $.post('ajax_utility/getShippingCostProdDtls/', { country_id: $(this).val(), 
                                                      product_id: <?php echo $this->uri->segment(3); ?>,
                                                      current_currency: '<?php echo $product->currency->cur_id; ?>'}, function(data){   alert("Successful ajax, but chrome is not reaching here");

        if(data.status != 1){
              $('#shippingRateDisplay').html("Shipping rate not set for the specified country");
            }
        else{
            $("#shippingRateDisplay").html("");

            var conShip = '<br /><table width="100%" cellpadding="0" cellspacing="1" bgcolor="#cdcdcd" class="darbluelink" id="shippingDetails">'+
                                    '<tr>'+
                                        ' <td width="20%" bgcolor="#e9e8e8" class="darkblue text-center" style="padding:7px"><b>Country of delivery</b></td>'+
                                        '<td colspan="2" align="center" bgcolor="#e9e8e8" class="darkblue text-center" style="padding:7px"><b>Shipping cost</b><b></b></td>'+
                                        '<td width="24%" bgcolor="#e9e8e8" class="darkblue text-center" style="padding:7px"><b>Service used</b></td>'+
                                        '<td width="16%" bgcolor="#e9e8e8" class="darkblue text-center" style="padding:7px"><b>Estimated shipping time</b></td>'+
                                    '</tr>'+
                                 '</table>';



            var shippingDtl = data.data.service_name == "Any Other" ? data.data.service_any:data.data.service_name;

            var tr = '<tr id="rowShippingDetails'+data.data.id+'">'+
                         '<td bgcolor="#FFFFFF" style="padding:7px" class="text-center">'+data.data.country_name+'</td>'+
                         '<td bgcolor="#FFFFFF" style="padding:7px" class="text-center"><span class="font2a1">For one quantity</span><br />'+data.data.cost_1_qty+'</td>'+
                         '<td bgcolor="#FFFFFF" style="padding:7px" class="text-center"><span class="font2a1">Each additional qty.</span><br />'+data.data.cost_many_qty+'</td>'+
                         '<td bgcolor="#FFFFFF" style="padding:7px" class="text-center">'+shippingDtl+'</td>'+
                         '<td bgcolor="#FFFFFF" style="padding:7px" class="text-center">'+data.data.shipping_time+' day(s)</td>'+
                      '</tr>';           

            $('#shippingRateDisplay').append(conShip);
            $('#shippingDetails').append(tr);   
        }

        }, 'json');
    });
});

This code basically fetches the shipping details for the country selected in the drop down and displays it in a table. The table has two rows, the first row has the column headings(like country, rate, shipping type etc) and the second row display the shipping data returned from the server.

A more cleaner code if I remove the table header and row creation code would be:

 //select the shipping country to display its shipping rate
  $('#country').change(function() {

     if($(this).val() == ""){
          $('#shippingRateDisplay').html('<br /><span style="margin-left:8px">No country selected.</span>');
          return false;
        }
        alert("Before ajax start");
    $.post('ajax_utility/getShippingCostProdDtls/', { country_id: $(this).val(), 
                                                      product_id: <?php echo $this->uri->segment(3); ?>,
                                                      current_currency: '<?php echo $product->currency->cur_id; ?>'}, function(data){   alert("Successful ajax, but chrome is not reaching here");

        if(data.status != 1){
              $('#shippingRateDisplay').html("Shipping rate not set for the specified country");
            }
        else{
            $("#shippingRateDisplay").html("");

            var conShip = 'THE_TABLE_HEADER';



            var shippingDtl = data.data.service_name == "Any Other" ? data.data.service_any:data.data.service_name;

            var tr = 'THE SHIPPING ROW CREATED WITH THE JSON DATA';          

            $('#shippingRateDisplay').append(conShip);
            $('#shippingDetails').append(tr);   
        }

        }, 'json');
    });
});

One of general response from the server is:

{
   "data":{
      "id":"4e6a274043ca1",
      "product_id":"131315437838",
      "country":"60",
      "cost_1_qty":"$ 5.00 CAD",
      "cost_many_qty":"$ 5.00 CAD",
      "service_used":"7",
      "service_any":"",
      "shipping_time":"5",
      "country_name":"French Guiana",
      "service_name":"Express\/Expedited International Shipping"
   },
   "status":1

}

The issue with this code is it works fine in IE and FF but has strange behavior in Chrome. The issue is , it worked fine for the first few times, then it could not. I restarted my machine and xampp, then again the same behavior, it displayed the shipping table for the first time when I selected a country from the drop down and from the second time it did not respond at all. I check the chrome firebug debugger, the post request was successful and it returned a 200 ok response along with the json data. But it failed to fire the second alert, which means it did not enter the callback function(which is executed if the request succeeds).

Also just to mention I am not setting the content type for the response that is being returned from the server. SO its text/html.

The following are the request and response headers

ResponseHeaders

Date: Sun, 11 Mar 2012 14:48:34 GMT
X-Powered-By: PHP/5.2.1
Connection: Keep-Alive
Content-Length: 282
Pragma: no-cache
Last-Modified: Sun, 11 Mar 2012 14:48:34 GMT
Server: Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8d mod_autoindex_color PHP/5.2.1
Content-Type: text/html
cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Keep-Alive: timeout=5, max=99
Expires: Thu, 19 Nov 1981 08:52:00 GMT

RequestHeaders

Content-Type: application/x-www-form-urlencoded
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*

Any help on why the call back function is not being executed, after the ajax call, and hence the shipping table not being displayed would be greatly helpful. Thank you.

UPDATE: I updated the code to use $.ajax, below is my code:

$.ajax({ type: "post",
url: 'ajax_utility/getShippingCostProdDtls/',
timeout : 5000,
data: { country_id: $(this).val(),
product_id: '<?php echo $this->uri->segment(3); ?>',
current_currency: '<?php echo $product->currency->cur_id; ?>'},
dataType: "json",
success: displayShippingTable,
error: AjaxFailed
});

` function AjaxFailed(result){
alert("FAILED : " + result.status + ' ' + result.statusText);
alert(result.responseText);
// displayShippingTable(result.responseText);

}  

`

This works fine on Firefox and IE , but on chrome its stranger than before. When I select or change a country in the shipping drop down, the console first says

POST ajax_utility/getShippingCostProdDtls/ 200 OK 108ms

then after 5 secs timeout it says

POST ajax_utility/getShippingCostProdDtls/ Aborted 108ms

Its then entering AjaxFailed function and is giving the alert FAILED : 200 OK and the json data returned from the server

It would be wonderful if somebody can help me to understand what is happening over and under the hood. Thank you.

Upvotes: 0

Views: 2401

Answers (1)

Amitabh
Amitabh

Reputation: 739

OK, finally I solved it myself. Well not exactly solved though. One important thing I forgot to mention here was the application I was working on was using jquery 1.3.2. I thought well, lets use the latest jquery for this page so that I can try out the promise interface.

And when I re run the page by including the jquery jquery-1.7.1.min.js , the problem immediately disappeared. The shipping table was loading absolutely fine. So I guess its due to a bug or incompatibility between jquery version and the chrome. As such in my case upgrading the jquery version fixed the problem.

Upvotes: 1

Related Questions