Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

json - jQuery's .getJSON using local files stopped working on Firefox 3.6.13

Suddenly, local file access using jQuery's .getJSON is not working for me on Firefox (3.6.13) - only.

I am aware of the problem in getting this to work in Chrome - Problems with jQuery getJSON using local files in Chrome. Using the option --allow-file-access-from-files fixes that. I now have code that works on IE, Chrome (as above) and used to work on Firefox, until now.

To give you some context:

   // get the only json file 
   var script = _.detect($('script').toArray().reverse(), function(script) {
      return script.type === 'application/json';
    });
    // script.src is now '../../json/foo/foo1.json'
    return $.getJSON(script.src, function(json) {
      alert('hello');// says 'hello' in IE, Chrome (with arg), used to work in Firefox
    });

Specifically, as the small code snippet shows, the .getJSON call fails to return and say 'hello' - the json file in question is on the local file system (eg file:///d/foo/bla.json). The JSON passes JSONLint validator.

I've tried disabling all addons - I also have a VM with Ubuntu & the installation of Firefox installed on it (3.6.13) has the same issue - worked a few days ago, now doesn't. Nothing in my code has changed since it was last working, to my knowledge. I just know someone is going to slap me for forgetting something really dumb.

Firefox shows no errors. That this works on Chrome and IE rules out most obvious mistakes.

Any hints or tips as to why this might suddenly stop working on Firefox, would be greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this is due to a new security feature in Firefox: You can make local requests only to files that are in the page's directory, or in the sub-directory of the page.

From here (emphasis mine):

In Gecko 1.8 or earlier, any two file: URIs are considered to be same-origin. In other words, any HTML file on your local disk can read any other file on your local disk.

Starting in Gecko 1.9, files are allowed to read only certain other files. Specifically, a file can read another file only if the parent directory of the originating file is an ancestor directory of the target file. Directories cannot be loaded this way, however.

For example, if you have a file foo.html which accesses another file, bar.html, the load will succeed only if bar.html is either in the same directory as foo.html or in a directory contained within the same directory as foo.html.


Footnote from PandaWood: I can confirm setting Firefox's about:config -> security.fileuri.strict_origin_policy to false, as the link you provide shows, is an immediate fix

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...