jQuery .getJSON Firefox 3 Syntax Error Undefined

2019-01-18 05:25发布

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=?

11条回答
趁早两清
2楼-- · 2019-01-18 05:59

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-18 06:00

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.

查看更多
仙女界的扛把子
4楼-- · 2019-01-18 06:02

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.

查看更多
Juvenile、少年°
5楼-- · 2019-01-18 06:09

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
查看更多
Evening l夕情丶
6楼-- · 2019-01-18 06:10

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.

查看更多
登录 后发表回答