简体   繁体   English

如何在IE9中使用ajax启用加载本地文件

[英]How to enable loading local file using ajax in IE9

I know there is the origin problem, but setting up a web server is not an option here. 我知道存在原因问题,但是这里不能选择设置Web服务器。 Firefox v14 has no problem loading a local file. Firefox v14加载本地文件没有问题。 Chrome has no problem after adding '--allow-file-access-from-files' Is there any way to fix also IE9? 添加'--allow-file-access-from-files'之后Chrome没有问题吗?还有什么方法可以修复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. 只需使用ActiveXObject(“MSXML2.XMLHTTP.6.0”)代替XMLHttpRequest()即可克服本地文件访问拒绝问题。

Ajax or not. Ajax与否。 HTTP is a client-server application protocol. HTTP是客户端 - 服务器应用程序协议。 Without a server, that's just not possible . 没有服务器,这是不可能的

UPDATE: 更新:

Possible in chrome (and firefox) apparently. 可能在chrome(和firefox)中显然。 As for IE you can read up on Mark of the Web . 至于IE,你可以阅读Mark of the Web

So far as I know, 据我所知,

  1. Considering security issues, javascript had better not access local files. 考虑到安全问题,javascript最好不要访问本地文件。 So it can't be standard. 所以它不能成为标准。

  2. In AJAX, there are respective ways to accessing local files for respective browsers. 在AJAX中,有各种方法可以访问各个浏览器的本地文件。

  3. For IE, as you seem to already know, while declaring an AJAX object initially, you should use new ActiveXObject() instead. 对于IE,正如您似乎已经知道的那样,在最初声明一个AJAX对象时,您应该使用new ActiveXObject()

  4. The AJAX of the JavaScript library JQuery allow you to access local files. JavaScript库JQuery的AJAX允许您访问本地文件。 I think it implemented all the ways for different browsers, eg, ActiveXObject for IE. 我认为它实现了不同浏览器的所有方式,例如ActiveXObject for IE。 AJAX of JQuery is very easy to use; JQuery的AJAX非常易于使用; 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. 由于JQuery包装了所有内容,因此对于访问您网站的人来说,JQuery可能会很危险。

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

ref: http://jquery.tiddlywiki.org/twFile.html (tell you the ways JQuery implementing access to local files) ref: http//jquery.tiddlywiki.org/twFile.html (告诉你JQuery实现对本地文件的访问的方式)

In case you're using requirejs 's text plugin , all you have to do is add this to the first require.config argument: 如果你正在使用requirejs文本插件 ,你所要做的就是将它添加到第一个require.config参数:

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

Maybe other JS libs use a similar syntax. 也许其他JS库使用类似的语法。 Food for thought. 值得深思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM