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

JavaScript

Robert Torres
Robert Torres
2,736 Points

How 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
jag
18,266 Points

You 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
Robert Torres
2,736 Points

Not 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
jag
18,266 Points

You 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
Robert Torres
2,736 Points

That worked! I knew it was some something simple! Thank you very much!