2

I need to create a html page which takes json data from a website and displays it.

Currently my code is this, which obviously doesn't do the job the way I wanted to but I wanted a starting point to display information from a page. Can anyone explain how I can go about this?

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
(window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","demo",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Click Button to display information!</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

1 Answer 1

2

So this is how you can do it:

This link will show you how to manipulate more in detail: http://jsfiddle.net/9zfHE/8/

$(document).ready(function(){
  $("#button").click(function(){
    $.getJSON("http://example.com/",function(result){
      $.each(result, function(i, field){
        $("#myDiv").append("<tr id='bord'><td>"+i + "</td><td> " + field.name + "</td><td>" + field.description + "</td></tr>");
      });
    });
  });
});

This link is exactly what you want as per it looks: http://jsfiddle.net/9zfHE/10/

$(document).ready(function(){
  $("#button").click(function(){
    $.getJSON("http://example.com/",function(result){
      $.each(result, function(i, field){
        $("#myDiv").append("<h4>" + field.name + "</h4><p>" + field.description + "</p>");
      });
    });
  });
});

You have to use ajax & .getJSON method which basically grabs fields from a url. Then I appended a table grabbing data from your json to the div.

If you add more data to your json then you can grab it by field.[json-field-name] and also give any style you like by providing 'id' to it & then styling in css. i keeps track of the number of elements in the json file.

Hope this helps

More documentation on: http://api.jquery.com/jquery.getjson/

Ajax uses a jquery plugin which needs to be embedded in your page for the .getJSON method to work.

[EDIT:] This is how your html file looks like:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#button").click(function(){
    $.getJSON("http://example.com/",function(result){
      $.each(result, function(i, field){
        $("#myDiv").append("<h4>" + field.name + "</h4><p>" + field.description + "</p>");
      });
    });
  });
});
</script>
</head>
<body>

<div id="myDiv"><h2>Click Button to display information!</h2></div>
<input type="submit" id="button" value="Change Content"></input>

<div></div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Whoa! Thanks a lot dude! I think I get it now :D
I edited the OP to what I think is how it should look using your example (Going to modify it myself later), sorry to bother you again but I'm still a little confused on how the end result should look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.