且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么jQuery.parseJSON()在所有服务器上都不起作用?

更新时间:2023-11-06 09:16:40

发现了问题!很难注意到,但我发现某事对该开口很有趣大括号...附近似乎有几个小点.我使用此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.