How to enable loading local file using ajax in IE9

无人久伴 提交于 2019-12-23 09:43:51

问题


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 overcome the local file access deny problem.


回答1:


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.




回答2:


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.org/twFile.html (tell you the ways JQuery implementing access to local files)




回答3:


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.



来源:https://stackoverflow.com/questions/11897086/how-to-enable-loading-local-file-using-ajax-in-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!