2017-01-12 81 views
0

GitHub的GitHub鏈接在這裏:https://github.com/Giphy/GiphyAPIGiphy API - 如何顯示來自搜索結果的圖像?

我的問題:由於我是新人,我現在有點卡住了。我從giphy API獲取隨機gif,而不是用戶想要的特定搜索結果。

如何獲得用戶想要的特定搜索結果而不是隨機gif?

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> 

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.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(); 
}); 

回答

0

在你的js代碼段你請求一個隨機的結果,而不是做成文本的搜索。在請求的URL是(注意隨機關鍵字)

http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag= 

,而不是像(從giphy文檔拍攝)

http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC 
+0

嘿,我寫的是這樣的代碼片段:request.open( 'GET','http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC');不起作用。 – spidey677

+0

request.open('GET','http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC'); 不起作用 – spidey677

+0

你對「不起作用」是什麼意思?你仍然得到隨機圖片? – Gnomo