简体   繁体   中英

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. The error does not halt the execution (hence a minor bug)

Example of xyz.json :

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

Although above is a valid JSON, XMLHttpRequest logs a syntax error at [

It is possible to avoid the error logging by setting responseType to 'text' .

Is this limited to Firefox Addon or Firefox or JavaScript in general?

Firefox will consult the response MIME type to see if the response should be parsed as 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 . This will of course fail if the file does not contain XML, and hence the warning.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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