0

我正在尝试在 javascript 中读取本地文件,我没有在线发布此文件,因此我只需要它与我的浏览器(Firefox)一起使用,我目前正在尝试使用 XML Http 请求解析它:

var txtFile = new XMLHttpRequest();
        txtFile.open("GET", "file://Users/spe_eddy_gonzalez/Dropbox/Me/Hon Proj/Wikipedia/simplewikitext.txt", true);
        txtFile.onreadystatechange = function() {
    if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
            if (txtFile.status === 200) {  // Makes sure it's found the file.
            allText = txtFile.responseText;
            lines = txtFile.responseText.split("\r\n"); // Will separate each line into an array
        } //"\r\n" 
    }
}
console.log($(txtFile).val());

var stringT = (new XMLSerializer()).serializeToString(txtFile);

但得到以下错误:

[11:22:43.970] NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 0 [nsIDOMSerializer.serializeToString] @ http://127.0.0.1:8020/CharCount2/character_counter2.js:26\

任何帮助都会非常受欢迎。我正在考虑用 Python 重写我的整个系统,因为我正在努力解决 Javascript 有限的 I/O 功能,而且我在 Python 方面有更多的经验

4

1 回答 1

3

Ajax 不适用于 file:// 协议。你应该在你的机器上使用一个简单的网络服务器,比如 Apache 或 Nginx。

于 2013-03-02T11:43:17.997 回答