【问题标题】:jQuery $.ajax request receives wrong response in Internet ExplorerjQuery $.ajax 请求在 Internet Explorer 中收到错误响应
【发布时间】:2012-09-12 09:02:45
【问题描述】:

我遇到了 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'); 
                }   
            }
        });
    }

PHP函数返回:

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

这一切在 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});,但结果是一样的。

有人遇到过这个问题吗?或者任何提示?
我没有更多的想法。

【问题讨论】:

    标签: php jquery json internet-explorer opencart


    【解决方案1】:

    尝试使用url: 'index.php?route=checkout/cart/add' 的绝对网址

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

    jQuery 需要 json。所以这是一个解析错误,导致错误回调而不是预期的成功。

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

    【讨论】:

    • 谢谢。事实证明,由于 URL 重写,相对路径指向一个不存在的位置。切换到绝对网址解决了这个问题。
    【解决方案2】:

    我让它在我所有的模组上工作的方式是使用

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

    使用代码,因此您的代码的 ajax 将是(小摘录)

        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',
    

    这具有始终是完整 URL 的好处,并且无论您使用 HTTP 还是 HTTPS 都可以正常工作

    【讨论】:

    • 这是一个很酷的解决方法。真的比在众多文件中硬编码 url 更好。没有考虑在 javascript 中处理 url。不错。
    • 当然,将基数放在变量中是个好主意,但jQuery('base') 似乎为我返回了一个空数组...window.location.origin 但确实对我有用。这是我的测试fiddle
    • @VDP - 大多数(如果不是所有 OC 商店主题)都设置了 BASE url,所以我怀疑这是您的代码无法正常工作的原因
    • 是的!你是对的。我忘了它使用了 OpenCart。 (我不熟悉那个库)我的错误:)
    【解决方案3】:

    我不喜欢像 Jay Gilford 在这里提供的那样在每次加载时都执行不必要的东西。改用这个:

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

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 2011-05-17
      相关资源
      最近更新 更多