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

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

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

问题

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

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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Cat Gif</title>
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1>This is a random image of a cat!</h1>
  11. </div>
  12. <script src="catGif.js">
  13. </script>
  14. </body>
  15. </html>

这是我的JavaScript:

  1. let container = document.querySelector(".container");
  2. async function apiFunction() {
  3. await fetch("https://api.thecatapi.com/v1/images/search")
  4. .then(res => res.json())
  5. .then((result) => {
  6. let img = document.createElement("img");
  7. img.src = result[0].url;
  8. container.appendChild(img);
  9. }),
  10. (error) => {
  11. console.log(error);
  12. }
  13. }
英文:

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)

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;title&gt;Cat Gif&lt;/title&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;div class=&quot;container&quot;&gt;
  10. &lt;h1&gt;This is a random image of a cat!&lt;/h1&gt;
  11. &lt;/div&gt;
  12. &lt;script src=&quot;catGif.js&quot;&gt;
  13. &lt;/script&gt;
  14. &lt;/body&gt;
  15. &lt;/html&gt;

//This is my javascript

  1. let container = document.querySelector(&quot;.container&quot;);
  2. async function apiFunction() {
  3. await fetch(&quot;https://api.thecatapi.com/v1/images/search&quot;)
  4. .then(res =&gt; res.json())
  5. .then((result) =&gt; {
  6. //items = result;
  7. let img = document.createElement(&quot;img&quot;);
  8. img.src = result[0].url;
  9. container.appendChild(img);
  10. }),
  11. (error) =&gt; {
  12. console.log(error);
  13. }
  14. }

答案1

得分: 2

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

  1. <!-- begin snippet: js hide: false console: true babel: false -->
  2. <!-- language: lang-html -->
  3. <!DOCTYPE html>
  4. <html lang="en">
  5. <head>
  6. <meta charset="UTF-8">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Cat Gif</title>
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1>This is a random image of a cat!</h1>
  13. </div>
  14. <script>
  15. let container = document.querySelector(".container");
  16. async function apiFunction() {
  17. await fetch("https://api.thecatapi.com/v1/images/search")
  18. .then(res => res.json())
  19. .then((result) => {
  20. //items = result;
  21. let img = document.createElement("img");
  22. img.src = result[0].url;
  23. container.appendChild(img);
  24. }),
  25. (error) => {
  26. console.log(error);
  27. }
  28. }
  29. // Call the function
  30. apiFunction();
  31. </script>
  32. </body>
  33. </html>
  34. <!-- end snippet -->

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

英文:

You did not call your defined function.

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

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;title&gt;Cat Gif&lt;/title&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;div class=&quot;container&quot;&gt;
  10. &lt;h1&gt;This is a random image of a cat!&lt;/h1&gt;
  11. &lt;/div&gt;
  12. &lt;script&gt;
  13. let container = document.querySelector(&quot;.container&quot;);
  14. async function apiFunction() {
  15. await fetch(&quot;https://api.thecatapi.com/v1/images/search&quot;)
  16. .then(res =&gt; res.json())
  17. .then((result) =&gt; {
  18. //items = result;
  19. let img = document.createElement(&quot;img&quot;);
  20. img.src = result[0].url;
  21. container.appendChild(img);
  22. }),
  23. (error) =&gt; {
  24. console.log(error);
  25. }
  26. }
  27. // Call the function
  28. apiFunction();
  29. &lt;/script&gt;
  30. &lt;/body&gt;
  31. &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:

确定