javascript - 如何使用 Giphy API 编写用于搜索的 gif 网格?
问题描述
我有几个问题...
我有一个用于搜索栏的 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);
})
});
发送的获取 URL 是
{...}/search?q= [object%20M...seEvent] &api_key={...}
我错过了什么?
另外,我不知道如何将 附加response.json
到 a<div id="results_grid"></div>
以返回 gif 网格。
谢谢你的帮助!
解决方案
您将按钮对象而不是搜索栏值发送到 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); }) });
推荐阅读
- c# - 如何在 Windows 10 下安装 svcutil.exe
- html - 加载新的 html5 视频时页面闪烁
- c# - 如何重新排序具有不同属性的测试方法,以便它们在代码中组合在一起?
- php - Symfony - 将数组的元素发布到数据库
- javascript - Javascript 如何从 php 文件响应中进行重定向
- css - 从 md-list-item (AngularJS Material) 中删除点击行为
- highcharts - Highcharts 显示带有文本的空白列
- docker - Docker 映像未构建:Linux 容器
- javascript - 如何根据元数据删除对切片的排序
- datatables - 在 parceljs 中导入数据表