javascript 从 .txt 文件加载数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9127447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading an array from .txt file
提问by David Debnar
I need to load an array from a .txt file. It has few strings separated by commas (,
).
我需要从 .txt 文件加载一个数组。它有几个由逗号 ( ,
)分隔的字符串。
So far I have found this.
到目前为止,我已经找到了这个。
That guy splits it with .split function but I have a problem. He uses an input to upload the file, however, I need to link the file to the HTML and than split it into an array and use it.
那家伙用 .split 函数将其拆分,但我遇到了问题。他使用输入上传文件,但是,我需要将文件链接到 HTML,然后将其拆分为数组并使用它。
回答by Alexander St?ver
Just make sure the text file is accessible through an HTTP request on the same domain and you can read it fine. Here's an example where the text file is located in the webroot.
只需确保文本文件可通过同一域上的 HTTP 请求访问,您就可以很好地阅读它。这是文本文件位于 webroot 中的示例。
$(function(){
$.get('/whatever.txt', function(data){
var array = data.split(',');
console.log(array);
});
});