问题
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,
Considering security issues, javascript had better not access local files. So it can't be standard.
In AJAX, there are respective ways to accessing local files for respective browsers.
For IE, as you seem to already know, while declaring an AJAX object initially, you should use
new ActiveXObject()
instead.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