Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
James Barrett
13,253 PointsTrying to retrieve information from the OMDb API
Hi there,
After completing the AJAX Basics course, Dave recommended to reinforce my learning by trying to retrieve information from the OMDb API (http://www.omdbapi.com/). Here is my code:
index.php
<form>
<label for="search">Type a search term</label>
<input type="search" name="search" id="search">
<input type="submit" value="Search" id="submit">
</form>
<div id="details">
</div>
$('form').submit(function (e) {
e.preventDefault();
var searchInput = $('#search');
var searchTerm = searchInput.val();
var submitButton = $('#submit');
submitButton.prop('disabled', true).val("Loading");
searchInput.prop('disabled', true);
// the AJAX part
var movieAPI = "http://www.omdbapi.com/?";
var movieOptions = {
s: searchTerm
};
function displayMovies(data) {
if($.isEmptyObject(data)) {
$('#details').html('<p>' + searchTerm + 'is not a valid search term! </p>');
submitButton.prop('disabled', false).val("Search");
searchInput.prop('disabled', false);
} else {
var listHTML = '<ul>';
$.each(data.search, function(i,movie) {
listHTML += '<p>' + movie.Title + '</p>'
}); // end each
listHTML += '</ul>';
$('#details').html(listHTML);
submitButton.prop('disabled', false).val("Search");
searchInput.prop('disabled', false);
}
}
$.getJSON(movieAPI, movieOptions, displayMovies);
}); // end click
When I submit the form, the GET request is successful. However nothing is displayed in the div? Where am I going wrong in my logic?
Thanks, James.
2 Answers
Ford Heacock
18,068 PointsGlad you learned the lesson on case-sensitivity! Can't tell you how many times I was stumped with my code when I was first learning..ha!
gregsmith5
32,615 PointsChange the <p> tags to <li> in the loop.
James Barrett
13,253 PointsTurns out JavaScript is unforgiving with case-sensitivity. Turns out it was because 'search' should be 'Search'!