我试图显示一个API中的文本,这是一个.txt文件。这可能与
async function load_url() {
let response = await fetch("<txt file>");
但我不知道其他的该怎么做。
另外,在线检索某一行的代码是什么?比如说,在一个.txt文件中,我怎么才能只检索到 "鱼 "这个字?
Cat
Fish
Giraffe
Bison
你应该使用json文件,我们需要知道你的后台使用哪种语言。
编辑 :
如果API给你返回一个文本文件,你可以这样做。
var lines = this.response.split('\n');
for(var line = 0; line < lines.length; line++){
console.log(lines[line]);
//If you want to show a specific line, just give the correct index
}
更新:这是我的新代码。
async function load_url() {
let response = await fetch("https://patrickmcneill.com/itp/urls.txt");
var lines = this.response.split('\n');
for(var line = 0; line < lines.length; line++){
console.log(lines[line]);
}
}
load_url();
但是我返回的是 "未抓到(在承诺中)TypeError.Cannot read property 'split' of undefined at load_url 无法读取load_url处未定义的'split'属性".