2

I'm receiving the following error message in Firefox:

Error: not well-formed
Source File: http://school/courses.booking.add.php?1287657494723
Line: 1, Column: 1
Source Code:
{"type":"error","message":"You have already booked this course."}

As you can see, the output is valid JSON (it's created by PHP's json_encode() function). And it's served with the application/json MIME type. I thought the error might be something to do with the parsing:

eval: function(json) {
    return eval('(' + json + ')');
}

But even if I don't parse the string, and simply alert the returned JSON response, the error still shows up.

Related Question: "not well-formed" error in Firefox when loading JSON file with XMLHttpRequest. His solution was to fix the MIME type. Mine's already accurate, so it must be something else.

Community
  • 1
  • 1
TRiG
  • 10,148
  • 7
  • 57
  • 107
  • Are you, by any chance, using any beta version of FF4 + FireBug? I had some weird problems with those... – Yuval Adam Oct 21 '10 at 10:53
  • FF3.6.11 + FireBug. I was using the error collecting in the web developer toolbar for this, though. I should probably use FireBug more than I currently do. – TRiG Oct 21 '10 at 10:55
  • If you don't pull the data from the API, instead reading it from a variable `var myJSONObject = {"type":"error","message":"You have already booked this course."}` and then try to `eval(myJSONObject)` does it still give you the same error? – irishbuzz Oct 21 '10 at 11:05
  • If I read from an object without evaling it works: `var ret = {"type":"error","message":"You have already booked this course."};`. If I write it as a text string and eval it, it works: `var ret = '{"type":"error","message":"You have already booked this course."}'; ret = ajax.eval(ret);`. – TRiG Oct 21 '10 at 11:19
  • It actually works perfectly in Firefox and every other browser I've tested. So I can safely ignore the error messages. Still, it would be nice to know what's causing them. – TRiG Oct 21 '10 at 11:33
  • @Yuval. I also had some weird effect in FF4. Finaly I found a difference between FF3.6 and FF4. The "tab" are not accepted in the last version of FF. – Zitun Feb 20 '11 at 13:37

3 Answers3

3

It seems that the javascript debugger in the Web Developer Toolbar simply expects all Ajax responses to be XML, regardless of the MIME type. Anything else will produce a "not well formed" error.

TRiG
  • 10,148
  • 7
  • 57
  • 107
0

I had this issue with previous versions of FireFox + FireBug where there existed newlines in front of/after the JSON formatted content. Make sure you clear your output stream before outputting JSON responses on the server side.

JSP example:

out.clear(); out.println(json);
ndtreviv
  • 3,473
  • 1
  • 31
  • 45
0

I had the same issue using OpenJS's jxs. What was causing the error, in this case, was this conditional in the load property (line 33 in version 3.01.A):

//XML Format need this for some Mozilla Browsers
if (http.overrideMimeType) http.overrideMimeType('text/xml');

It made the browser always expect for XML. That can be easily fixed by this:

// XML Format needs this for some Mozilla Browsers
if (format.charAt(0) === "x" && http.overrideMimeType) http.overrideMimeType("text/xml");

Since it now makes a comparison of format, the code should also have its place changed and should be put after

format = format.toLowerCase();

Which is currently at line 38. So, the code goes from:

32 //XML Format need this for some Mozilla Browsers
33 if (http.overrideMimeType) http.overrideMimeType('text/xml');
34
35 if(!method) method = "GET";//Default method is GET
36 if(!format) format = "text";//Default return type is 'text'
37 if(!opt) opt = {};
38 format = format.toLowerCase();
39 method = method.toUpperCase();

To:

32 if(!method) method = "GET";//Default method is GET
33 if(!format) format = "text";//Default return type is 'text'
34 if(!opt) opt = {};
35 format = format.toLowerCase();
36 method = method.toUpperCase();
37
38 //XML Format need this for some Mozilla Browsers
39 if (format.charAt(0) === "x" && http.overrideMimeType) http.overrideMimeType("text/xml");
RPF
  • 16