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 trialRobert Torres
2,736 PointsHow to print api data to html page
Trying to figure how I can get this data to print on a html page.
var $xhr = $.getJSON('http://www.omdbapi.com/?t=Wolverine');
$xhr.done(function(data) { if ($xhr.status !== 200) { return; }; console.log(data); });
3 Answers
jag
18,266 PointsYou were missing some code.
// Pure JS
var link = "http://www.omdbapi.com/?t=Wolverine'"
function getData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var obj = JSON.parse(this.response);
document.write(obj.Title);
}
};
xhttp.open("GET", link, true);
xhttp.send();
}
getData();
//And if you want it to be simple using jQuery.
$.getJSON(link)
.done(function(data){
console.log(data.Title)
})
Robert Torres
2,736 PointsNot exactly what I am looking for but thanks. I am trying to print the data from json to show on a web page. Like actor name , birth etc....
jag
18,266 PointsYou are able to use the code to print it on the webpage. With selectors you can add them into their proper tags.
// Print title of movie
document.write(obj.Title);
// If we have a div for title
document.getElementById("title").innerHTML = obj.Title;
// In jQuery
$('#data').html(data.Title);
Robert Torres
2,736 PointsThat worked! I knew it was some something simple! Thank you very much!