简体   繁体   English

Firefox插件:XMLHttpRequest读取JSON的小错误

[英]Firefox Addon: XMLHttpRequest minor bug reading JSON

I have noticed that XMLHttpRequest in my Firefox addon logs an error processing xyz.json if content is an array. 我已经注意到,如果content是数组,我的Firefox插件中的XMLHttpRequest会记录处理xyz.json的错误。 The error does not halt the execution (hence a minor bug) 该错误不会停止执行(因此是一个小错误)

Example of xyz.json : xyz.json的示例:

[
  "string1",
  "string2",
  "string3"
]

Although above is a valid JSON, XMLHttpRequest logs a syntax error at [ 尽管以上是有效的JSON,但XMLHttpRequest会[

It is possible to avoid the error logging by setting responseType to 'text' . 通过将responseType设置为'text'可以避免错误记录。

Is this limited to Firefox Addon or Firefox or JavaScript in general? 通常,这是否仅限于Firefox插件或Firefox或JavaScript?

Firefox will consult the response MIME type to see if the response should be parsed as XML. Firefox将查询响应的MIME类型,以查看响应是否应解析为XML。 Reading some file: , chrome: , resource: , etc. that does not provide a response MIME type will make XHR default to XML (backwards-compatibility; after all the XML is right in the name), and attempt to parse it to provide .responseXML . 读取一些不提供响应MIME类型的file:chrome:resource:等,将使XHR默认为XML(向后兼容;毕竟XML名称正确),并尝试解析它以提供.responseXML This will of course fail if the file does not contain XML, and hence the warning. 如果该文件不包含XML,那么这当然会失败,因此会出现警告。

Use .overrideMimeType() to provide an explicit proper MIME type, or just text/plain if in doubt, to stop XHR from attempting to parse data as XML, and the message will go away. 使用.overrideMimeType()提供明确的正确MIME类型,或者如果有疑问,则仅提供text/plain ,以阻止XHR尝试将数据解析为XML,消息将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM