4

I'm having issues with ajax json requests and Internet Explorer. Specifically ajax requests do not behave properly.
I'm using:
OpenCart 1.5.3.1
jquery-1.7.1.min.js
jquery-ui-1.8.16.custom.min.js
Internet Explorer 9
PHP 5.2.9

This is the request function:

    function addToCart(product_id, quantity, option_id, option_value) {
        quantity = typeof(quantity) != 'undefined' ? quantity : 1;
        option_value = typeof(option_value) != 'undefined' ? option_value : 0;
        option_id = typeof(option_id) != 'undefined' ? option_id : 0;
        jQuery.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            cache: false,
            data: 'product_id=' + product_id + '&quantity=' + quantity + '&option_id=' + option_id + '&option_value=' + option_value+'&rnd=' + Math.random(),
            dataType: 'json',
            success: function(jsonObj) {
                $('.success, .warning, .attention, .information, .error').remove();

                if (jsonObj['redirect']) {
                    location = jsonObj['redirect'];
                }

                if (jsonObj['success']) {
                    $('#notification').html('<div class="success" style="display: none;">' + jsonObj['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

                    $('.success').fadeIn('slow');

                    $('#cart-total').html(jsonObj['total']);

                    $('html, body').animate({ scrollTop: 0 }, 'slow'); 
                }   
            }
        });
    }

The PHP function return:

{"success":"Added to cart!","total":"1 product(s) - 52,48\u043b\u0432."}  

This all works fine in Chrome, FF etc. but fails in IE.

Actually IE does not fire a "success" event.
The only way I can get a response is through an error handler.
Then the json object has a status = 200 and statusText = OK

This is the json object after success event fired in Chrome:

jsonObj: Object
success: "Added to cart!"
total: "1 product(s) - 52.48лв."
__proto__: Object  

From which the 'success' and 'total' values are used.

This is the json object after the error event was handled in Internet Explorer:
Internet Explorer 9 developer tools screenshot

The responseText is a string that contains the current page html source.

I've tried with jQuery.ajaxSetup({cache: false}); but the result is the same.

Has anyone had this issue? Or any tips?
I have no more ideas.

4

3 回答 3

2

try using absolute url for url: 'index.php?route=checkout/cart/add'

the problem is your respons is html or xml (I see the start tag (namespace tag) of an xml page followed by a newline followed by the (old)html starting tag).

jQuery expects json. So it's a parse error resulting in an error callback in staid of the expected success.

Make sure the backend sends the right info and that the called page is right. You can use the network tab to capture the calls to find your problem.

于 2012-09-12T09:11:29.477 回答
2

The way I've gotten this to work on all of my mods is to use

$('base').attr('href')

with the code, so the ajax for your code would be (small exerpt)

    option_value = typeof(option_value) != 'undefined' ? option_value : 0;
    option_id = typeof(option_id) != 'undefined' ? option_id : 0;
    var base = jQuery('base').attr('href');
    jQuery.ajax({
        url: base + 'index.php?route=checkout/cart/add',
        type: 'post',

This has the benefit of always being a full URL and also working regardless of you using HTTP or HTTPS

于 2012-09-12T12:06:47.340 回答
0

I don't like having unnecessary stuff executed on every load as Jay Gilford offered here. Use this instead:

if ($.browser.msie) {
    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        options.url = $('base').attr('href') + options.url;
    });
}
于 2013-11-26T07:40:30.837 回答