相关疑难解决方法(0)

jQuery:从文件系统中读取文本文件

我试图使用jquery读取文本文件,如下所示:

// 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>');
    });
});
Run Code Online (Sandbox Code Playgroud)

在chrome中,我得到:

XMLHttpRequest cannot load file:///C:/myPath/myFile.txt. Origin null is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)

Firefox没有显示错误,但代码没有执行(我在firebug中有断点,匿名函数从不运行).

任何帮助赞赏!

编辑:

必须:

  • 使用文件完整路径
  • 使用"--allow-file-access-from-files"启动chrome

现在它工作正常!

filesystems jquery get file

15
推荐指数
3
解决办法
10万
查看次数

标签 统计

file ×1

filesystems ×1

get ×1

jquery ×1