为什么jQuery.parseJSON()在所有服务器上都不起作用? [英] Why doesn't jQuery.parseJSON() work on all servers?

查看:102
本文介绍了为什么jQuery.parseJSON()在所有服务器上都不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个阿拉伯语联系人脚本,该脚本使用Ajax在填写表格后从服务器检索响应.

Hey there, I have an Arabic contact script that uses Ajax to retrieve a response from the server after filling the form.

在某些apache服务器上,jQuery.parseJSON()对其完全解析在其他服务器上的json抛出invalid json例外.仅在chrome和IE上抛出此异常.

On some apache servers, jQuery.parseJSON() throws an invalid json excepion for the same json it parses perfectly on other servers. This exception is thrown only on chrome and IE.

使用php的json_encode()函数对json内容进行编码.我尝试使用json数据发送正确的标头,并将unicode设置为utf-8,但这无济于事.

The json content gets encoded using php's json_encode() function. I tried sending the correct header with the json data and setting the unicode to utf-8, but that didn't help.

这是我尝试解析的json响应之一(删除了if的第二部分,因为它很长):

This is one of the json responses I try to parse (removed the second part of if because it's long):

{"pageTitle":"\u062e\u0637\u0623 \u0639\u0646\u062f \u0627\u0644\u0625\u0631\u0633\u0627\u0644 !"}

注意:此数据的语言为阿拉伯语,这就是为什么在用php的json_encode()进行解析后看起来像这样.

Note: This language of this data is Arabic, that's why it looks like this after being parsed with php's json_encode().

您可以尝试在下面给出的示例中提出请求,并使用firebug或webkit开发人员工具查看完整的响应数据.响应通过 jsonlint

You can try to make a request in the examples given down and look at the full response data using firebug or webkit developer tools. The response passes jsonlint!

最后,我有两个使用相同版本脚本的URL,尝试使用chrome或IE浏览它们,以查看损坏示例中的错误.

Finally, I have two urls using the same version of the script, try to browse them using chrome or IE to see the error in the broken example.

工作示例: http://namodg.com/n/

残破的示例: http://www.mt-is.co.cc/my/call-me/

更新:为了澄清更多信息,我想指出的是,我使用旧的eval()解析内容来解决此问题,我发布了与此修复程序一起发布的另一个版本,就像这个:

Updated: To clarify more, I would like to note that I manged to fix this by using the old eval() to parse the content, I released another version with this fix, it was like this:

// Parse the JSON data
try
{
    // Use jquery's default parser
    data = $.parseJSON(data);
}
catch(e)
{
    /*
     * Fix a bug where strange unicode chars in the json data makes the jQuery
     * parseJSON() throw an error (only on some servers), by using the old eval() - slower though!
     */
    data = eval( "(" + data + ")" );
}

我仍然想知道这是否是jquery的parseJSON()方法中的错误,以便我可以向他们报告.

I still want to know if this is a bug in jquery's parseJSON() method, so that I can report it to them.

推荐答案

发现了问题!很难注意到,但我发现某事对该开口很有趣大括号...附近似乎有几个小点.我使用此JavaScript小书签来了解它是什么:

Found the problem! It was very hard to notice, but I saw something funny about that opening brace... there seemed to be a couple of little dots near it. I used this JavaScript bookmarklet to find out what it was:

javascript:window.location='http://www.google.com/search?q=u+'+('000'+prompt('String?').charCodeAt(prompt('Index?')).toString(16)).slice(-4)

我得到了结果页面.猜猜是什么问题!在输出的开头有一个看不见的字符,实际上重复了两次.零宽度不间断空格也称为 Unicode字节顺序标记(BOM).这就是jQuery拒绝您原本有效的JSON以及将JSON粘贴到JSONLint中的秘密工作原理(取决于您的操作方式)的原因.

I got the results page. Guess what the problem is! There is an invisible character, repeated twice actually, at the beginning of your output. The zero width non-breaking space is also called the Unicode byte order mark (BOM). It is the reason why jQuery is rejecting your otherwise valid JSON and why pasting the JSON into JSONLint mysteriously works (depending on how you do it).

使这个不需要的字符进入输出的一种方法是在UTF-8模式下使用Windows记事本保存PHP文件!如果您正在这样做,请使用另一个文本编辑器,例如 Notepad ++ .重新保存所有没有PHP的PHP文件,以解决问题.

One way to get this unwanted character into your output is to save your PHP files using Windows Notepad in UTF-8 mode! If this is what you are doing, get another text editor such as Notepad++. Resave all your PHP files without the BOM to fix your problem.

步骤1:默认情况下,设置记事本++以UTF-8编码文件而无需BOM.

Step 1: Set up Notepad++ to encode files in UTF-8 without BOM by default.

步骤2:打开每个现有的PHP文件,更改编码"设置,然后重新保存.

Step 2: Open each existing PHP file, change the Encoding setting, and resave it.

这篇关于为什么jQuery.parseJSON()在所有服务器上都不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆