我的問題: 看起來我的代碼在某處不正確。搜索不起作用。我是使用API的新手。GIPHY API - 如何搜索Giphs?
如何獲取用戶搜索的特定搜索結果?
GitHub鏈接到Giphy API在這裏: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>
我仍然無法得到它的工作。你能幫我多一點嗎? – spidey677
你是否必須使用'request = new XMLHttpRequest;'? –
還是你打開使用jQuery? –