开发者

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

开发者 https://www.devze.com 2023-02-05 07:13 出处:网络
Suddenly, local file access using jQuery\'s .getJSON is not working for me on Firefox (3.6.13) - only.

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.


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


0

精彩评论

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