"not well-formed" error in Firefox when loading JSON file with XMLHttpRequest "not well-formed" error in Firefox when loading JSON file with XMLHttpRequest javascript javascript

"not well-formed" error in Firefox when loading JSON file with XMLHttpRequest


Have you tried using the MIME type for JSON?

application/json

You could also configure your server to send this MIME type automatically for .json files.


Firstly, true JSON is much stricter than JavaScript, and to be valid JSON, you have to have your keys quoted.

 { "a": 3 } 

Also, as you are using a bare XMLHttpRequest, which generally expects to receive an XML result unless MIME headers specify strictly otherwise.

You may however wish to make your own life easier by simply using a JavaScript framework such as jQuery which will abstract away all this problem for you and deal with all the nasty edge cases.

$.getJSON("data.json",{}, function( data ){   /*  # do stuff here  */ });

Additionally, if you use both strict JSON and use a library to abstract it for you, when browsers start having native JSON parsers the library will be able to transparently make use of these and get a significant speed improvement.

( This is slated to happen sooner than later, and when it happens, your users will get a silent upgrade with no effort required! ).


This also occurs when Content-Type is completely empty (thereby circumventing the natural type-detection).