【问题标题】:How to code a gif grid for a search using the Giphy API?如何使用 Giphy API 编写用于搜索的 gif 网格?
【发布时间】:2021-07-27 10:10:48
【问题描述】:

我有几个问题...

我有一个用于搜索栏的 HTML 表单,我试图将它发送到 Giphy API,但是当我为提交按钮编写 addEventListener("click") 代码时,我无法获取用户在输入标签上输入的字符串.

HTML:

<form id="input_bar"> <input type="text" name="search_string" id="search_string"> <button type="submit" id="search_submit">Submit!</button> </form>

JS:

const searchBar = document.getElementById("search_string"); const submitBtn = document.getElementById("search_submit");

submitBtn.addEventListener("click", function getResults(submitBtn) { var searchResult = fetch("https://api.giphy.com/v1/gifs/search?q=" + submitBtn + "&api_key=" + apiKey) .then(function(response) { console.log(response); }) });

发送的抓取网址是

{...}/search?q=[object%20M…seEvent]&api_key={...}

我错过了什么?

另外,我不知道如何将 response.json 附加到 &lt;div id="results_grid"&gt;&lt;/div&gt; 以返回 gif 网格。

感谢您的帮助!

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您将按钮对象而不是搜索栏值发送到 API。

    改变这个:

    searchResult = fetch("https://api.giphy.com/v1/gifs/search?q=" + submitBtn + "&api_key=" + apiKey) .then(function(response) { console.log(response); }) });
    

    到这里:

    searchResult = fetch("https://api.giphy.com/v1/gifs/search?q=" + searchBar.value + "&api_key=" + apiKey) .then(function(response) { console.log(response); }) });
    

    【讨论】:

      猜你喜欢
      • 2017-05-28
      • 2020-08-20
      • 2017-05-31
      • 1970-01-01
      • 2019-07-26
      • 2020-02-14
      • 2018-12-02
      • 2017-08-15
      • 1970-01-01
      相关资源
      最近更新 更多