views:

5112

answers:

12

I'm getting a syntax error (undefined line 1 test.js) in Firefox 3 when I run this code. The alert works properly (it displays 'work') but I have no idea why I am receiving the syntax error.

jQuery code:

$.getJSON("json/test.js", function(data) {
    alert(data[0].test);
});

test.js:

[{"test": "work"}]

Any ideas? I'm working on this for a larger .js file but I've narrowed it down to this code. What's crazy is if I replace the local file with a remote path there is no syntax error (here's an example):

http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?

A: 

Try renaming "test.js" to "test.json", which is what Wikipedia says is the official extension for JSON files. Maybe it's being processed as Javascript at some point.

sblundy
Nope, that didn't do it. Same syntax error (undefined line 1 test.json)
Mike
A: 

Have you tried disabling all the Firefox extensions?

I usually get some errors in the Firebug console that are caused by the extensions, not by the webs being visited.

Serhii
Yep, I've tried disabling all add-ons and still get the error via the error console.
Mike
+1  A: 

This may sound really really dumb, but change the file extension for test.js from .js to .txt. I had the same thing happen with perfectly valid JSON data files with pretty well any extension except .txt (example: .json, .i18n). Since I've changed the extension, I get the data and use it just fine.

Like I said, it may sound dumb but it worked for me.

Manik
Tried changing to a .txt extension and it still is giving me the error. I'm able to get all the data but receive the error. Luckily when placed on an actual web server (as opposed to running locally) it works fine (no error).
Mike
+1  A: 

The first answer is wrong: Both the following are valid JSON.

{"test":"work"}    
[{"test": "work"}]

The first one is a object (record/struct), the second is an array containing only one value (namely the object above).

I have the same problem as the original author and I am still seeking for the reason. As he note, everything keeps working - only the syntax error is logged. Even more: There is no way to tell where this syntax error happened...

Steffen Heil
A: 

Is anyone having an answere on that issue? I'm currently experiencing the very same error. Is there something I can do?

I thought it might have to do with the wrong MIME-Type beeing send, but I have no proof for it...

A: 

Check if there's ; at the end of the test.js. jQuery executes eval("(" + data + ")") and semicolon would prevent Firefox from finding closing parenthesis. And there might be some other unseen characters that prevents it from doing so.

I can tell you why this remote location working though, it's because it's executed in completely different manner. Since it has jsoncallback=? as the part of query parameters, jQuery thinks of it as of JSONP and actually inserts it into the DOM inside <script> tags. Try use "json/test.js?callback=?" as target, it might help too.

vava
A: 

What kind of webserver are you running that on? I once had an issue reading a JSON file on IIS because it wasn't defined as a valid MIME type.

cdmckay
+5  A: 

I found a solution to kick that error

$.ajaxSetup({'beforeSend': function(xhr){
    if (xhr.overrideMimeType)
        xhr.overrideMimeType("text/plain");
    }
});

Now the explanation: In firefox 3 (and I asume only firefox THREE) every file that has the mime-type of "text/xml" is parsed and syntax-checked. If you start your JSON with an "[" it will raise an Syntax Error, if it starts with "{" it's an "Malformed Error" (my translation for "nicht wohlgeformt"). If I access my json-file from an local script - no server is included in this progress - I have to override the mime-type... Maybe you set your MIME-Type for that very file wrong...

How ever, adding this little piece of code will save you from an error-message

It also works to override the content type on the server side using content-type header
Ian McLaird
A: 

Try configuring the content type of the .js file. Firefox expects it to be text/plain, apparently. You can do that as Peter Hoffmann does above, or you can set the content type header server side.

This might mean a server-side configuration change (like apache's mime.types file), or if the json is served from a script, setting the content-type header in the script.

Or at least that seems to have made the error go away for me.

Ian McLaird
A: 

I had a similar problem but was looping through a for loop. I think the problem might be that the index is out of bound.

  • Kien
KienPham.com
A: 

HI

I have this same error when testing the web page on my local PC, but once it is up on the hosting server the error no longer happens. Sorry - I have no idea of the reason, but thought it may help someone else track down the reason

Natalie
A: 

getJSON may be insisting on at least one name:value pair.
A straight array ["item0","item1","Item2"] is valid JSON, but there's nothing to reference it with in the callback function for getJSON.

In this little array of Zip codes:

{"result":[["43001","ALEXANDRIA"],["43002","AMLIN"],["43003","ASHLEY"],["43004","BLACKLICK"],["43005","BLADENSBURG"],["43006","BRINKHAVEN"]]}

... I was stuck until I added the {"result": tag. Afterward I could reference it:

$.getJSON("temp_test_json.php","", function(data) { $.each(data.result, function(i, item) { alert(item[0]+ " " + i); if (i > 4 ) return false; }); });

... I also found it was just easier to use $.each().

Pete Zicari