如何在我的网页上使用JavaScript和HTML中的async-await显示来自API的随机猫图片?

huangapple go评论73阅读模式
英文:

How can I display a random cat image from an API on my webpage using async-await in JavaScript and HTML?

问题

我正在编写一个程序,它调用一个API以获取一张随机的猫咪图片。我想要能够在我的网页上显示这张图片,而不仅仅在控制台中显示URL。

这是我的HTML(非常基本,但我只想让输出正常工作):

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cat Gif</title>
</head>

<body>
    <div class="container">
        <h1>This is a random image of a cat!</h1>
    </div>

    <script src="catGif.js">
    </script>
</body>

</html>

这是我的JavaScript:

let container = document.querySelector(".container");
async function apiFunction() {
    await fetch("https://api.thecatapi.com/v1/images/search")
        .then(res => res.json())
        .then((result) => {
            let img = document.createElement("img");
            img.src = result[0].url;
            container.appendChild(img);
        }),
        (error) => {
            console.log(error);
        }
}
英文:

I am writing a program which calls from an API to get a random cat image. I want to be able to display that image on my webpage and not just display the url in the console.

//This is my html (It is really basic but I just want to get the output to work)

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Cat Gif&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;h1&gt;This is a random image of a cat!&lt;/h1&gt;
    &lt;/div&gt;

    &lt;script src=&quot;catGif.js&quot;&gt;
    &lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

//This is my javascript

let container = document.querySelector(&quot;.container&quot;);
    async function apiFunction() {
        await fetch(&quot;https://api.thecatapi.com/v1/images/search&quot;)
            .then(res =&gt; res.json())
            .then((result) =&gt; {
                //items = result;
                let img = document.createElement(&quot;img&quot;);
                img.src = result[0].url;
                container.appendChild(img);

            }),
            (error) =&gt; {
                console.log(error);
            }
    }

答案1

得分: 2

你没有调用你定义的函数。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cat Gif</title>
</head>

<body>
    <div class="container">
        <h1>This is a random image of a cat!</h1>
    </div>

    <script>
        let container = document.querySelector(".container");
        async function apiFunction() {
            await fetch("https://api.thecatapi.com/v1/images/search")
                .then(res => res.json())
                .then((result) => {
                    //items = result;
                    let img = document.createElement("img");
                    img.src = result[0].url;
                    container.appendChild(img);
                }),
                (error) => {
                    console.log(error);
                }
        }
        // Call the function
        apiFunction();
    </script>
</body>

</html>

<!-- end snippet -->

如果你需要进一步的帮助,请告诉我。

英文:

You did not call your defined function.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Cat Gif&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;h1&gt;This is a random image of a cat!&lt;/h1&gt;
    &lt;/div&gt;

    &lt;script&gt;
        let container = document.querySelector(&quot;.container&quot;);
        async function apiFunction() {
            await fetch(&quot;https://api.thecatapi.com/v1/images/search&quot;)
                .then(res =&gt; res.json())
                .then((result) =&gt; {
                    //items = result;
                    let img = document.createElement(&quot;img&quot;);
                    img.src = result[0].url;
                    container.appendChild(img);
                }),
                (error) =&gt; {
                    console.log(error);
                }
        }
        // Call the function
        apiFunction();
    &lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 15:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定