How to enable loading local file using ajax in IE9

9.4k Views Asked by At

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.

3

There are 3 best solutions below

2
On

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.

0
On

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)

0
On

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.