Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get JSON data from an external URL?

Tags:

json

html

jquery

A company that I am working with has an API for their Privacy Policy and Terms of Use. I need to create two different pages...one for the Privacy Policy and one for the Terms of Use. I'm pretty new at this, and I feel like I'm so close.

Here is the way that the JSON is received (with some edits to anonymize it):

{"code":200,"status":"Ok","data":
    {"offset":0,"limit":20,"total":2,"target":"html_page","results":[
        {
            "id":"6",
            "title":"Privacy Policy",
            "description":"Privacy Policy",
            "html":"HTML CODE BLAH BLAH",
            "tags":["privacy"]
        },
        {
            "id":"66",
            "title":"License and TOU",
            "description":"Terms of Use",
            "html":"HTML CODE BLAH BLAH",
            "tags":["terms"]
        }]
    }
}

Here's the code I'm using:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Privacy Policy</title>
</head>
<body>
    <div id="placeholder"></div>
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script>
    $.getJSON('http://externalurl.com', function(external) {
        var output="<ul>";
        for (var i in external.code.offset) {
            output+="<li>" + external.html + "</li>";
        }

        output+="</ul>";
        document.getElementById("placeholder").innerHTML=output;
  });
    </script>
</body>
</html>

It's just returning a blank page. What am I doing wrong?

like image 315
Drew Avatar asked May 12 '13 01:05

Drew


Video Answer


1 Answers

$.getJSON('http://externalurl.com', function(external) {
        var output = $("<ul />");

        $.each(external.data.results, function(i, result) {
             $('<li />', {text : result.html}).appendTo(output);
        });

        $('#placeholder').html(output);
});
like image 130
adeneo Avatar answered Sep 30 '22 04:09

adeneo