My issue:
I’m having trouble with my GIPHY API integration. The search feature doesn’t seem to be working properly and I think there might be an error in my implementation. I’m still learning how to work with APIs so I could really use some guidance.
What I’m trying to achieve:
I want users to be able to search for specific GIFs and display the results on my webpage. Right now the search isn’t functioning as expected.
JavaScript code:
document.addEventListener('DOMContentLoaded', function () {
//searchTerm = "";
xhrRequest = new XMLHttpRequest;
xhrRequest.open('GET', 'http://api.giphy.com/v1/gifs/search?q=dancing+dog&api_key=dc6zaTOxFJmzC');
xhrRequest.onload = function() {
if (xhrRequest.status >= 200 && xhrRequest.status < 400){
response = JSON.parse(xhrRequest.responseText).data.image_url;
console.log(response);
document.getElementById("gif_display").innerHTML = '<div><img src = "'+response+'" title="GIF from Giphy"></div>';
} else {
console.log('API request failed with error');
}
};
xhrRequest.onerror = function() {
console.log('network connection failed');
};
xhrRequest.send();
});
HTML markup:
<h2> Find Amazing GIFs! </h2>
<div class="search-container">
<p> Enter your search term below to discover awesome GIFs! </p>
<form class="search-form">
<input type="text" id="search-term" class="input-field">
<button type="submit" class="find_button"> Find GIFs </button>
<input type="reset" value="Clear">
</form>
<div class="results_area animated fadeIn">
<p id="gif_display"> </p>
</div>
</div>