开发者

Firefox JSON "not well formed" errror on valid JSON

开发者 https://www.devze.com 2023-01-21 20:47 出处:网络
I\'m receiving the following error message in Firefox: Error: not well-formed Source File: http://school/courses.booking.add.php?1287657494723

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 JS开发者_C百科ON (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.


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.


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");


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号