我已经通过 PowerShell 创建了一个 JSON 文件并将其放在服务器上。
当我通过 $.getJSON 访问该 JOSN 文件时,它在 crome 和 IE 浏览器中工作正常,但是当我在 Firefox 中访问该 JSON 文件时,出现错误
JSON.parse: unexpected character at line 1 column 1 of the JSON data
标题:
响应:
应该是什么问题以及如何在 firefox 中修复它?
最佳答案
您已经说过服务器使用 Content-Type: text/plain
发回该 JSON。数据似乎是 UTF-16(可能是基于屏幕截图),但 text/plain
的默认字符集是 us-ascii
(参见 §4.1. RFC2046 中的 2 个):
4.1.2. Charset Parameter
A critical parameter that may be specified in the Content-Type field for "text/plain" data is the character set. This is specified with a "charset" parameter, as in:
Content-type: text/plain; charset=iso-8859-1
Unlike some other parameter values, the values of the charset parameter are NOT case sensitive. The default character set, which must be assumed in the absence of a charset parameter, is US-ASCII.
因此,您需要更改服务器的响应,使其正确识别正在使用的字符集,例如内容类型:文本/纯文本; charset=UTF-16
(显然首先要确保那实际上是资源的字符集)。
我只是注意到,从我可以从 JSON 中得出的结论来看,它看起来主要是在西方脚本中。如果是这样,UTF-16 是不寻常且低效的选择,您可能最好使用 UTF-8。但我只有一小部分文本可以使用。
关于javascript - JSON.解析 : unexpected character at line 1 column 1 of the JSON data in Firefox only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402326/