gpt4 book ai didi

javascript - GIPHY API - 如何搜索 Gif?

转载 作者:行者123 更新时间:2023-11-28 18:10:50 25 4
gpt4 key购买 nike

我的问题:看来我的代码在某个地方不正确。搜索不起作用。我是使用 API 的新手。

如何获取用户搜索的具体搜索结果?

Giphy API 的 GitHub 链接位于:https://github.com/Giphy/GiphyAPI .

JS

document.addEventListener('DOMContentLoaded', function () {

//q = "";

request = new XMLHttpRequest;
//request.open('GET', 'http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag='+q, true);
request.open('GET', 'http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC');


request.onload = function() {
if (request.status >= 200 && request.status < 400){
data = JSON.parse(request.responseText).data.image_url;
console.log(data);
document.getElementById("here_is_gif").innerHTML = '<center><img src = "'+data+'" title="GIF via Giphy"></center>';
} else {
console.log('reached giphy, but API returned an error');
}
};

request.onerror = function() {
console.log('connection error');
};

request.send();
});

HTML

<h1> Let's Search Some Gifs! </h1>
<div class="info">
<p> Search below to the wonderful world of Gifs! </p>
<form class="gif-form">
<input type="text" id="form-value" class="search-input-rounded">
<button type="submit" class="search_button"> Search for GIFs </button>
<input type="reset" value="Reset">
</form>
<div class="rando_facts animated bounceIn">
<p id="here_is_gif"> </p>
</div>
</div>

最佳答案

这是一个使用 jQuery 的解决方案。有关更多信息,请参阅他们的文档... http://api.jquery.com/

另请注意,Giphy API 会回复图像的 URL,而不是图像本身。因此,<img>标签替换为<p>你原来用过的。使用src属性来使用 url 渲染图像。

检查浏览器的控制台日志以查看 API 响应的外观。

下面是完整的 HTML,请注意,jQuery 必须始终链接到您的代码上方。

<!DOCTYPE html>
<html>
<head>
<title>Giphy App</title>
</head>
<body>
<h1> Let's Search Some Gifs! </h1>
<div class="info">
<p> Search below to the wonderful world of Gifs! </p>
<form class="gif-form">
<input type="text" id="form-value" class="search-input-rounded">
<button type="submit" class="search_button"> Search for GIFs </button>
<input type="reset" id="reset_button" value="Reset">
</form>
<div class="rando_facts animated bounceIn">
<img id="here_is_gif" src=""/>
</div>
</div>

<!-- Link in jQuery from CDN -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

<!-- My JavaScript -->
<script type="text/javascript">

// This waits for the page to load before calling our jQuery
$( document ).ready(function(){

// Part 1 - Collect User Input Using jQuery Click Listener note we use the class (.) of search_button
$('.search_button').on('click', function(){

// Collect user by grabbing the input form's value via id (#)
var userInput = $('#form-value').val().trim();

// Change the input to suit the API (ie change spaces to +)
userInput = userInput.replace(/ /g, "+");

// Create the Giphy API URL
var queryURL = 'http://api.giphy.com/v1/gifs/search?q=' + userInput + '&api_key=dc6zaTOxFJmzC';

// Part 2 - Use AJAX to call GIPHY API (note that the .done() function waits for the API to respond)
$.ajax({url: queryURL, method: 'GET'}).done(function(response){

// This is the API response data. It's a JSON object of 25 gifs
console.log(response.data);

// For simplicity, we will take the first gif (ie. at postion 0)
var giphyURL = response.data[0].images.fixed_height.url;
console.log(giphyURL)

// Now you can pass that into your "here_is_gif" <img> tag using its id (#)
$('#here_is_gif').attr('src', giphyURL);

});

// Part 3 - Clear the Gif using the reset_button id (#)
$('#reset_button').on('click', function(){
// Grab the img using the id and change the src to empty to remove the image
$('#here_is_gif').attr("src",'');
})


// If using a jQuery click listner on a Submit button, you need to return false to prevent the default page refresh
return false;
})


});

</script>

</body>
</html>

关于javascript - GIPHY API - 如何搜索 Gif?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com