简体   繁体   中英

How can I read local files from JavaScript with Firefox

I have some standalone JavaScript code that I've written to use with MaxMSP (a visual language for MIDI and DSP) that I need to debug.

I downloaded WebStorm to try it out. When I try to execute code that reads a local file, I get the following error:

Exception... "Security error" code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)"

I know people have seen this before and I have tried all the suggestions I found to bypass security and enable local file reading from Firefox but I'm still not able to get this to work.

I'm wondering if something has changed in recent versions of Firefox that make all the old suggestions useless.

Would appreciate any suggestions.

Thanks, David

OK --- I figured this out! The big problem for me is lack of knowledge of javascript libraries.

The XMLHttpRequest works but it needs an extra line

xmlhttp.send();

I didn't know about that. So code that actually works to read data from a local file (assuming one has followed the instructions posted in other places to turn off security is as simple as

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", filename, false); // Synchronous
xmlhttp.send();
var text = xmlhttp.responseText;

I hope this saves others some time --- it was a real PITA for me to figure this out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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