jQuery:从文件系统读取文本文件 [英] jQuery: read text file from file system
问题描述
// LOAD文件并逐行拆分并附加div
$ .get('myFile.txt',function(data){
var lines = data.split(\\\
);
$。 each(lines,function(n,elem){
$('#myContainer')。append('< div>'+ elem +'< / div>');
});
});
在chrome中,我得到:
XMLHttpRequest无法加载文件:/// C:/myPath/myFile.txt。 Access-Control-Allow-Origin不允许Origin null。
Firefox显示没有错误,但代码未执行(我在firebug中有断点,匿名功能从不
任何帮助赞赏!
编辑:
不得不:
- 使用文件完整路径
- 启动
chrome with
--allow-file-access-from-files
now它的工作正常!
您不能从本地文件系统加载文件,像这样,你需要把它放在一个web服务器,并从那里加载它。在与您的JavaScript相同的网站上。
编辑:看这个线程,您可以使用选项启动chrome - allow-file-access-from-files
,这将允许访问本地文件。
I am trying to read a text file using jquery, like this:
// LOAD file and split line by line and append divs
$.get('myFile.txt', function(data) {
var lines = data.split("\n");
$.each(lines, function(n, elem) {
$('#myContainer').append('<div>' + elem + '</div>');
});
});
In chrome, I am getting:
XMLHttpRequest cannot load file:///C:/myPath/myFile.txt. Origin null is not allowed by Access-Control-Allow-Origin.
Firefox shows no error but the code is not executed (I have breakpoints in firebug and the anonymous function never runs).
Any help appreciated!
EDIT:
had to:
- use the file full path
- launch chrome with "--allow-file-access-from-files"
now it's working OK!
You can't load a file from your local filesystem, like this, you need to put it on a a web server and load it from there. On the same site as you have the JavaScript loaded from.
EDIT: Looking at this thread, you can start chrome using option --allow-file-access-from-files
, which would allow access to local files.
这篇关于jQuery:从文件系统读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!