使用 Fetch Api 进行 Giphy 搜索

mKe01010101

在使用 Api 时,我很新。我正在尝试查询 URL 字符串并返回一些 Gif。

const gifForm = document.querySelector("#gif-form");
gifForm.addEventListener("submit", fetchGiphs);

function fetchGiphs(e) {
  e.preventDefault();
  const searchTerm = document.querySelector("#search").value;
  fetch(`https://api.giphy.com/v1/gifs/search?&q=${searchTerm}&limit=100&api_key=3mIxmBZUIIPyb8R69gtxaW8Hsh74dFKV`)
  .then((response) => {return response.json(); })
  .then(data => showGiphs(data.images.fixed_width.url))
  .then(err => console.log(err));
}

function showGiphs(fixed_width) {
  const results = document.querySelector("#results");

  let output = '<div class="container">';
  fixed_width.forEach((url) => {
    console.log(url);
    
    output += `
  <img src="${data.images.original.fixed_width_url}"/>

`;
  });
  document.getElementById('results').innerHTML = output;
}
<form id="gif-form">
  <input type="text" id="search">
  <input type="submit" value="find">
</form>
<div id="results"></div>

如果我.then(data => showGiphs(data.images.fixed_width.url))从 fetch 中删除并且只删除console.log数据,它将返回我想要的搜索结果。但是,当我尝试映射到 gif"data.images.fixed_width.url" 时,我收到控制台错误“Uncaught (in promise) TypeError: Cannot read property 'fixed_width' of undefined”fetch.then.then.data

任何帮助或朝正确方向推动都会很棒!谢谢!另外,如果您想查看演示,可以在此处查看:https : //codepen.io/Brushel/pen/jKgEXO?editors=1010

优素福BH

您的代码有几个问题。

API 的响应是一个对象,在这个对象中有一个data数组,这个数组中的每个元素都是关于每个 gif 的信息。

尝试:

const gifForm = document.querySelector("#gif-form");
gifForm.addEventListener("submit", fetchGiphs);

function fetchGiphs(e) {
    e.preventDefault();
    const searchTerm = document.querySelector(".search").value;
    fetch(`https://api.giphy.com/v1/gifs/search?&q=${searchTerm}&limit=100&api_key=3mIxmBZUIIPyb8R69gtxaW8Hsh74dFKV`)
    .then((response) => {return response.json(); })
    .then((resp => {
        // Here we get the data array from the response object
        let dataArray = resp.data
        // We pass the array to showGiphs function
        showGiphs(dataArray);
    }))
    .catch(err => console.log(err)); // We use catch method for Error handling
}

function showGiphs(dataArray) {
  const results = document.querySelector(".results");
  let output = '<div class="container">';
  dataArray.forEach((imgData) => {
    output += `
  <img src="${imgData.images.fixed_width.url}"/>
`;
  });
  document.querySelector('.results').innerHTML = output;
}
    <form id="gif-form">
    <input type="text" class="search">
    <input type="submit" value="find">
</form>
<div class="results"></div>

我希望这将有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

提取Giphy API(添加搜索框)

来自分类Dev

提取Giphy API(添加搜索框)

来自分类Dev

如何使用jQuery和PHP在Giphy API中进行无限滚动?

来自分类Dev

尝试使用早期 api fetch 中的 id 进行 api fetch

来自分类Dev

使用Java Api进行弹性搜索查询

来自分类Dev

使用Graph API进行全字搜索?

来自分类Dev

使用Graph API进行全字搜索?

来自分类Dev

使用Google API进行图片搜索

来自分类Dev

使用Java Api进行弹性搜索查询

来自分类Dev

尝试使用React挂钩从Giphy API显示GIF

来自分类Dev

无法在我的Giphy API上使用Ajax调用

来自分类Dev

使用新的Fetch API对JSON进行跨域请求

来自分类Dev

如何使用fetch进行通用API调用功能

来自分类Dev

如何使用Azure Search API进行多索引搜索?

来自分类Dev

如何使用AppEngine和Search API按DocId进行搜索?

来自分类Dev

如何在企业中使用Github API进行搜索

来自分类Dev

使用URL API中的react.js进行实时搜索

来自分类Dev

使用Google Maps API(gmaps.js)进行地址搜索

来自分类Dev

如何使用Drupal 7 Search API在PHP中进行搜索?

来自分类Dev

使用Java API在Lucene中进行图像搜索

来自分类Dev

使用Or PowerShell进行搜索

来自分类Dev

使用Textarea进行搜索

来自分类Dev

使用参数进行搜索

来自分类Dev

使用Powershell进行搜索

来自分类Dev

使用 HashMap 进行搜索

来自分类Dev

无法遍历 Giphy API 数组

来自分类Dev

使用 fetch API 授权

来自分类Dev

使用Solr搜索进行高级搜索

来自分类Dev

使用Solr搜索进行高级搜索