Php jQuery$.ajax请求在Internet Explorer中收到错误响应

Php jQuery$.ajax请求在Internet Explorer中收到错误响应,php,jquery,json,internet-explorer,opencart,Php,Jquery,Json,Internet Explorer,Opencart,我在ajax json请求和Internet Explorer方面遇到问题。 特别是ajax请求的行为不正常。 我正在使用: 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 这是请求功能: function addToCart(product_id, quantity, option_id, option_value) { q

我在ajax json请求和Internet Explorer方面遇到问题。 特别是ajax请求的行为不正常。
我正在使用:
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

这是请求功能:

    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'); 
                }   
            }
        });
    }
这一切在Chrome、FF等中都很好,但在IE中却失败了

事实上,IE不会触发“成功”事件。
获得响应的唯一方法是通过错误处理程序。
然后json对象的status=200,statusText=OK

这是在Chrome中触发成功事件后的json对象:

jsonObj: Object
success: "Added to cart!"
total: "1 product(s) - 52.48лв."
__proto__: Object  
从中使用“成功”和“总计”值

这是在Internet Explorer中处理错误事件后的json对象:

responseText是包含当前页面html源的字符串

我尝试了
jQuery.ajaxSetup({cache:false})但结果是相同的

有人有这个问题吗?有什么建议吗?

我没有更多的想法。

尝试使用绝对url作为
url:'index.php?route=checkout/cart/add'

问题是您的响应是html或xml(我看到xml页面的开始标记(名称空间标记),后跟一个换行符,后跟(旧的)html开始标记)

jQuery需要json。所以这是一个解析错误,导致在预期成功的状态下进行错误回调


确保后端发送正确的信息,并且调用的页面是正确的。您可以使用“网络”选项卡捕获呼叫以查找您的问题。

我在所有MOD上都使用了这种方法

$('base').attr('href')
有了代码,您的代码的ajax将是(small experpt)


这样做的好处是始终是一个完整的URL,并且无论您使用HTTP或HTTPS,都可以正常工作。我不喜欢像Jay Gilford在这里提供的那样在每次加载时都执行不必要的内容。改用这个:

if ($.browser.msie) {
    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        options.url = $('base').attr('href') + options.url;
    });
}

谢谢你。结果表明,由于URL重写,相对路径指向一个不存在的位置。切换到绝对url解决了这个问题。这是一个很酷的解决方法。在很多文件中都比硬编码url好。没有考虑过用javascript处理url。很好。当然把基放在变量中是个好主意,但是
jQuery('base')
似乎为我返回了一个空数组<代码>窗口.位置.原点
但对我来说确实有效。这里是我的test@VDP-大多数(如果不是所有OC商店主题的话)都设置了基本url,所以我怀疑这就是你的代码不工作的原因!你说得对。我忘了它用的是OpenCart。(我不熟悉那个词)我的错误:)
    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',
if ($.browser.msie) {
    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        options.url = $('base').attr('href') + options.url;
    });
}