【问题标题】:Internet explorer 9 gives parseJSON errorInternet explorer 9 给出 parseJSON 错误
【发布时间】:2013-03-09 07:44:35
【问题描述】:

我不知道是 jQuery 还是我的脚本引起的。这是 Internet Explorer 9 出错的地方:

parseJSON: function( data ) {
    // Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        return window.JSON.parse( data ); <<< this the "Invalid Character" err.
    }

这是我的 JavaScript 代码

http://jsfiddle.net/fNPwh

根据我的研究,我还在这里检查了我的 DOCTYPE。他们在谈论 Quirks 模式,所以我也尝试输入 x-ua-compatible,但没有任何改变

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" > <!-- IE9 mode -->
blabla

为什么 ie9 给出 jQuery 的无效字符错误?而不是 Firefox 或 webkit。哦,顺便说一句,我使用的是 jQuery 1.9.1。

【问题讨论】:

  • 既然你用的是jQuery,为什么不把post()请求的dataType设置为json,让jQuery处理解析呢?
  • 你能给我举个例子吗?我正在使用我的数据,例如“x.name,x.page...”
  • api.jquery.com/jQuery.post 我查看了 dataType ,现在很酷我不需要使用 parseJSON 但我仍然想知道为什么 IE 给出错误:D
  • 我做了这个例子,但与此同时你想通了 - 如果其他人感兴趣,请点击这里的链接:jsfiddle.net/mbrowne/jqbUp。至于 IE 给出错误,它可能比您正在测试的其他浏览器更严格地遵循 JSON 规范。可能是您的 JSON 对象的键没有被引号包围,但很难说没有看到你的 JSON。
  • paste.laravel.com/jTl -> 这是我的 JSON

标签: jquery json internet-explorer-9 jquery-1.9


【解决方案1】:

这似乎是 jQuery 库中的一个错误。 This post 包含为修复添加/修改的代码。基本上只是在尝试解析之前移动对null 的检查并添加对undefined 的检查。方法的开始应该是这样的......

parseJSON: function( data ) {
    if (data === undefined) {
        return data;
    }
    if (data === null) {
        return data;
    }

    // Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        return window.JSON.parse( data );
    }
...

我已在 IE10 和 Chrome 中验证了此修复程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2013-03-16
    • 2013-06-06
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多