简体   繁体   中英

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

I'm trying to display texts from an API, that's a.txt file. It may have something to do with

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

But I do not know what to do for the rest.

Also what is the code for retrieving online a certain line? Like, how would I retrieve just "Fish" in a.txt file that's just this?

    Cat
    Fish
    Giraffe
    Bison

You should use a json file, Homewhere, we need to know which language do you use for your backend

Edit:

If the API retur you a text file, you can do that:

    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
    }

Edit:

You just dont have any response by your request

Update: Here's my new code:

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

But I'm returning "Uncaught (in promise) TypeError: Cannot read property 'split' of undefined at load_url" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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