2013-03-09 72 views
2

我不知道它是由jQuery还是由我的脚本引起的。这是Internet Explorer 9的给出错误:Internet Explorer 9给出parseJSON错误

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这里。他们在谈论怪癖模式,所以我也试图把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。

+1

由于您使用的是jQuery,为什么不将'post()'请求的'dataType'设置为'json'并让jQuery处理解析呢? – 2013-03-09 07:48:30

+0

你能告诉我一个例子吗?我正在使用我的数据,如'x.name,x.page ...' – 2013-03-09 07:49:34

+0

http://api.jquery.com/jQuery.post/我查看了dataType,这很酷现在我不需要使用parseJSON,但是我仍然想知道为什么IE给出了错误:D – 2013-03-09 07:55:02

回答

1

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

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中。