最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to enable loading local file using ajax in IE9 - Stack Overflow

programmeradmin0浏览0评论

I know there is the origin problem, but setting up a web server is not an option here. Firefox v14 has no problem loading a local file. Chrome has no problem after adding '--allow-file-access-from-files' Is there any way to fix also IE9? Thank you


Edit: I figured out the solution. Just use ActiveXObject("MSXML2.XMLHTTP.6.0") instead of XMLHttpRequest() for IE9 to overe the local file access deny problem.

I know there is the origin problem, but setting up a web server is not an option here. Firefox v14 has no problem loading a local file. Chrome has no problem after adding '--allow-file-access-from-files' Is there any way to fix also IE9? Thank you


Edit: I figured out the solution. Just use ActiveXObject("MSXML2.XMLHTTP.6.0") instead of XMLHttpRequest() for IE9 to overe the local file access deny problem.

Share Improve this question edited Aug 13, 2012 at 9:50 hippietrail 17k21 gold badges109 silver badges179 bronze badges asked Aug 10, 2012 at 7:18 user1589188user1589188 5,73621 gold badges77 silver badges153 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Ajax or not. HTTP is a client-server application protocol. Without a server, that's just not possible.

UPDATE:

Possible in chrome (and firefox) apparently. As for IE you can read up on Mark of the Web.

So far as I know,

  1. Considering security issues, javascript had better not access local files. So it can't be standard.

  2. In AJAX, there are respective ways to accessing local files for respective browsers.

  3. For IE, as you seem to already know, while declaring an AJAX object initially, you should use new ActiveXObject() instead.

  4. The AJAX of the JavaScript library JQuery allow you to access local files. I think it implemented all the ways for different browsers, e.g., ActiveXObject for IE. AJAX of JQuery is very easy to use; everyone likes it. But as mentioned above, there are security problems. Since JQuery wrapped it all, JQuery may be dangerous for who visit you site.

===================================================================

ref: http://jquery.tiddlywiki/twFile.html (tell you the ways JQuery implementing access to local files)

In case you're using requirejs's text plugin, all you have to do is add this to the first require.config argument:

requirejs.config({
  config: {
    text: {
      createXhr: function(){
        return new ActiveXObject("MSXML2.XMLHTTP.6.0");
      }
    }
  }
});

Maybe other JS libs use a similar syntax. Food for thought.

发布评论

评论列表(0)

  1. 暂无评论