繁体   English   中英

如何使用带有 jsonp 的异步 function 在 a.txt 文件中显示文本?

[英]How do I display text in a .txt file with an async function with jsonp?

我正在尝试显示来自 API 的文本,这是一个 .txt 文件。 它可能与

    async function load_url() {
    let response = await fetch("<txt file>");

但我不知道如何处理 rest。

另外在线检索某行的代码是什么? 就像,我将如何在一个 .txt 文件中仅检索“鱼”?

    Cat
    Fish
    Giraffe
    Bison

您应该使用 json 文件,Homewhere,我们需要知道您的后端使用哪种语言

编辑:

如果 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();

但我返回"Uncaught (in promise) TypeError: Cannot read property 'split' of undefined at load_url"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM