使用 xmlhttprequest 读取文件会导致错误控制台条目
我使用以下代码从 javascript 读取文件
var filePath = "SBL_PROBES.txt";
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",filePath,false);
xmlhttp.send(null);
var fileContent = xmlhttp.responseText;
var fileArray = fileContent.split('\n')
var n = fileArray.length;
一切顺利,我可以从 fileArray 访问文件内容。但是 Firefox 错误控制台上出现错误,抱怨文件第 1 行的内容。我不想要由 javascript 或 firefox 解释的文件,我想要的只是用 javascript 解析的内容。
Firefox 控制台显示
错误:语法错误 源文件:file:///C:/Documents%20and%20Settings/Mike/Desktop/mustache/SBL_PROBES.txt 行:1,列:1 源代码: "title" "Short Name" "Long Name" "Current","Maximum","Minimum","Day Max","Day Min"
如果我把 >在文件中,错误消失了!
发生了什么事以及如何修复它?
我需要执行任何操作来关闭该文件吗?当这些变量超出范围时,事情会被清理吗?
I used the following code to read a file from javascript
var filePath = "SBL_PROBES.txt";
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",filePath,false);
xmlhttp.send(null);
var fileContent = xmlhttp.responseText;
var fileArray = fileContent.split('\n')
var n = fileArray.length;
Things go fine and I can access the file contents from fileArray. BUT an error appears on the firefox error console complaining about the contents of line 1 of the file. I don't want the file interpreted by javascript or firefox, all I want are the contents which I parse with javascript.
The firefox console says
Error: syntax error
Source File: file:///C:/Documents%20and%20Settings/Mike/Desktop/mustache/SBL_PROBES.txt
Line: 1, Column: 1
Source Code:
"title" "Short Name" "Long Name" "Current","Maximum","Minimum","Day Max","Day Min"
If I put <blockquote> </blockquote
> in the file, the errors go away!
What's going on and how can I fix it?
Do I need to do anything to close the file? Will things be cleaned up when these vars go out of scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以解决这个问题:
默认情况下,本地文件似乎是使用 XMLParser 进行解析的。
This should fix it:
By default it seems that local files are parsed with the XMLParser.