views:

6710

answers:

5

I'm getting a "not well-formed" error in the error console of Firefox 3.0.7 when the JavaScript on my page loads a text file containing an object in JavaScript Object Notation format. If the file contains nothing but the JSON object, it produces the error. If I wrap the object in <document></document> tags it does not produce the error. The request succeeds either way, so I could just ignore it, but I don't want my error log filling up with these messages.

Here is some example code to illustrate the problem. First, the "not well-formed" file called "data.json":

{ a: 3 }

Now some code to load the file:

var req = new XMLHttpRequest();
req.open("GET", "data.json");
req.send(null);

Which produces the following error in the Firefox error console:

not well-formed
file://path/to/data.json Line: 1
{ a: 3 }
- ^

If data.json is modified to this:

<document>{ a: 3 }</document>

There is no error. I assumed that it is complaining because the plain JSON file is not a well formed XML document, so I tried overriding the MIME type before the "send" call to force it to load as plain text, but that didn't work.

var req = new XMLHttpRequest();
req.open("GET", "data.json");
req.overrideMimeType("text/plain");
req.send(null);
// Still produces an error!

I am going to continue with wrapping my JSON data in an XML document to get around whatever validation the XMLHttpRequest is performing, but I'd like to know if there is any way I can force it to just load plain text uncritically and not try to validate it. Alternatively, is there another method of loading data besides XMLHttpRequest that can be used with plain text?

+10  A: 

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.

jthompson
This works, thanks!
A. Levy
To be more specific: req.overrideMimeType("application/json"); req.send(null);works. Specifying the MIME type in the server would be the better solution, but overriding it works as well.
A. Levy
+6  A: 

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! ).

Kent Fredric
Thanks for the pointers on JSON. I will change my data to use quoted string attribute names. Unfortunately, jQuery, or some other framework, is not an option for me.
A. Levy
I'd be most interested why, generally that's the sign of code-stink to vehemently avoid code reuse and to avoid using solved problems.
Kent Fredric
this should be the real answer...
Hippo
@Kent, I'd be happy to use jQuery. However, the approval process for using jQuery is prohibitively difficult. The amount of time it would take to argue with the customer and demonstrate that we need to use jQuery is not worth the benefit. Sad, but true.
A. Levy
@A. Levy: you'll learn, and next time you'll be less likely to hand-code-everything :)
Kent Fredric
A: 

It should be {"a": 3} actually.

Julian Aubourg
A: 

{"items":[{"name": "A"},{"name": "B"},{"name": "C"}]}

Which is wrong with this? I received the same error

Leandro
You are new to StackOverflow, so you may not realize this yet, but you aren't supposed to ask another question in the "Answers" section. You should open a new question. That way more people will see your problem and answer it. No one is going to read your question stuck in the middle of the answers section on an old question I asked a year ago. I don't see anything particularly wrong with your example, so I can't help you. You need to start a new question and give more details on where you are seeing this error.
A. Levy