Loading...

jQuery Forum

When I load a JSON file locally, I receive the following error in my Firefox console: "not well-formed / file:///<fileLocation>/data.json / Line: 1 / {}". The loading otherwise seems successful, as I can interact with the loaded JavaScript object. For this example, I'm loading an empty JSON object, but this error occurs with non-empty JSON objects, too.

My JavaScript code embedded in HTML:

Copy code
  1. <script type="text/javascript" src="jquery-1.4.1.js"></script>
  2. <script type="text/javascript">
  3.     $.getJSON("data.json", function(jsonData, textStatus)
  4.     {
  5.         alert(textStatus);
  6.     });
  7. </script>

data.json:

Copy code
  1. {}

Is this an issue with my code, a jQuery issue, or a Firefox issue?

I'm using Firefox 3.6 and jQuery 1.4.1. This error does not occur in IE 7.

-- Mark

Replies(8)

Does anyone know a solution to this? I have been researching and trying different things for hours now. This seems really, really dumb. Even though the Content-Type is set to "application/json" (and I can tell the response's content-type is correct in Firebug), I am still getting an XML Parsing Error. Makes no sense at.
You should read this:


Where it says this:

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.
 

You should read the API a little bit before asking for help, don't ya think ?? ;-)
Cheers

I can't reproduce the problem with either 1.4.1 or 1.4.2 using Firefox 3.6.3. No errors, no console messages, and it returns the empty object from the local file.
I've read the API docs several times. The problem is, it's not a syntax error. Even if the JSON is a simple object like {"foo":"bar"}, I am getting the error. The only issue is the Firefox console error, everything functions properly. My jQuery code looks something like this:

var foo;
$.ajax({
      url: '/conf/foo.json',
      contentType: 'application/json',
      success: function(data){
            foo = data;
      }
});

btw I'm testing on FF 3.6.3 and jQuery 1.4.2
I located the source of this issue. Apparently my Tomcat instance did not have a MIME mapping for JSON. To fix this error, edit your "\conf\web.xml"  file under your Tomcat directory. Add the following config to this file:

<mime-mapping>
<extension>json</extension>
        <mime-type>application/json</mime-type>
</mime-mapping>
I am having this problem.

My JSON was output by Jason, a JSON editor and validator, AND I submitted it to a third party online validator (just in case 1uka.z wants to submit another completely unhelpful reply one year later).  As with the OP, the data loads just fine (1uka.z seems to have missed that little tidbit).

The file is local as with the OP (i havent' tried it from an http server yet).  FF version is 9.0.1.  Firebug 1.9

My code looks like this:

////////
$.getJSON("data.json", function(data, textStatus) {
if (textStatus == "success")
{
      alert("loaded");
}
else
alert("JSON non-success status: " + textStatus);
});
///////////

Even the the data loads, the error message is annoying.

I am also facing the same issue. In firefox only I am getting the console error "not well-formed". IE and chrome doesn't throw anything like this. My code is like 

$.ajax({
datatype : "json",
ContentType : "application/json",
url : "http://[hostname]:[port]/json/GetDetails",
data : {
'apikey' : 'f7764bf1c85af29c4ba823140deb0ad4',
'period' : 'today',
},
success : function(listOFMG) {
alert("JSON successfully loaded..");
}

      });

How to solve this problem? Its annoying me for few hours..

I doubt your problem is identical to these previous posters' problems. You should make your own question.

Is it because your JSON is invalid? 
Is it because you actually coded   "http://[hostname]:[port]/json/GetDetails"?
Is it because you did not serve your page from a server that is allowed to do cross domain access to that URL?




JΛ̊KE