【问题标题】:Jquery Ajax doesn't work at Internet ExplorerJquery Ajax 在 Internet Explorer 上不起作用
【发布时间】:2012-07-31 02:52:13
【问题描述】:

当我在 Internet Explorer 9 上使用 jQuery ajax 时,我在响应正文中获取数据,但它不会传递给成功函数。我怎样才能让它发挥作用?

示例:

...
    $.ajax({
        async : false,
        type: 'PUT',
        contentType: 'application/json',
        url: updateUrl,
        data: JSON.stringify(model),
        dataType: 'json',
        success: function(data) {
            console.log("Here!");//it comes here
            console.log(data);//it logs undefine at ie, firefox and etc is logging data
            r = resultResponse(data);
        },
        error: function(data) {
            try {
                r = error($.parseJSON(data.responseText));
            } catch (err) {
                //Handle error
            }
        }
    });
...

我调试了网络,看到响应正文是:

{"message":"Connection is successful","status":"success"}

但是在 Internet Explorer 的成功函数中数据未定义。

有什么想法吗?

PS 1:奇怪的是,当我从服务器发送数据而不为响应标头设置内容类型时它可以工作?

PS 2:我的响应头如下:

Key Value
Response    HTTP/1.1 200 OK
Server  Apache-Coyote/1.1
Content-Type    application/json;charset=UTF8
Transfer-Encoding   chunked
Date    Thu, 02 Aug 2012 15:50:44 GMT

【问题讨论】:

  • 你的响应的 CharacterEncoding 是什么?
  • @viyancs 我已经编辑了我的问题。
  • ajax 选项 "traditional: true" 可能有帮助
  • 它是只有 IE9 那里不起作用吗?
  • @nnnnnn 我没有用其他 Internet Explorer 版本测试它。

标签: ajax jquery cross-browser


【解决方案1】:

这可能有很多问题,但是您可以尝试将完整的 $.ajax 调用替换为 $.getJSON 吗? 浏览器执行起来会更加简单和容易,因为您的问题可能在于您为 ajax 调用所做的众多设置和配置之一。

尽量简单:

$.getJSON(updateUrl, JSON.stringify(model), function(data){
    console.log(data);
});

如果您需要查看 getJSON,请查看here

【讨论】:

    【解决方案2】:

    从输出中可以看出,我的字符集是 UTF8 而不是 UTF-8。问题出在服务器端。

    【讨论】:

      猜你喜欢
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多