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 trialhb8
5,660 Pointsjsonp pages
I'm a little stuck on how i can retrieve all data from a jsonp api when the data is split in pages. the callback looks like this
{
entries: [],
page: 1,
pages: 101,
posts: 10054
}
this code only gets me results from page 1, but i would like to get results from all 101 pages would appreciate some help :)
$.getJSON('http://hotell.difi.no/api/jsonp/mattilsynet/smilefjes/tilsyn?callback=?', function(data){
var html = "";
$.each(data.entries, function(key,value){
html += '<p>' + value.navn + '</p>';
})
$('#test').html(html);
})
5 Answers
Erik Nuber
20,629 Pointsthe file is an object that is an array of objects. so you need to use an index to locate each item and then use dot notation for the value you want.
This should work but, can't test it myself.
$.getJSON('http://hotell.difi.no/api/jsonp/mattilsynet/smilefjes/tilsyn?callback=?', function(data){
var html = "";
for (var i = 0; i < data.entries.length; i++) {
html += "<p>" + data.entries[i].navn + "</p>;
}
$('#test').html(html);
})
you should also be able to do...
var allEntries = data.entries;
for (var i in allEntries) {
html += "<p>" + allEntries[i].navn + "</p>;
}
hb8
5,660 PointsHi Erik, i think your code is the same as mine, only difference i'm using jquery. The code still only returns objects from page one.... If i add a query to the url like : &page=2 or &page=3 i can access objects from that page only, what i'm trying to is access all objects from all pages in one go....
Erik Nuber
20,629 PointsI looked at the data being sent back, ie the giant link in your .getJSON call. It is a large object with roughly 100 items. Is this just one page? Because based on how I wrote the code, It should be returning the .navn of all of those items.
hb8
5,660 Pointsyes, thats only one page :)
hb8
5,660 PointsI gat some help and i think this will work ...
$.getJSON('http://hotell.difi.no/api/jsonp/mattilsynet/smilefjes/tilsyn?callback=?', function(data) {
var pages = data.pages;
for (var i = 1; i <= data.pages; i++) {
$.getJSON('http://hotell.difi.no/api/jsonp/mattilsynet/smilefjes/tilsyn?callback=?&page=' + i, function(data) {
$('#test').append("Page " + data.page + " Data >> ");
var html = "";
$.each(data.entries, function(key, value) {
html += '<p>' + value.navn + '</p>';
})
$('#test').append(html);
});
}
});
Erik Nuber
20,629 PointsGotcha, than it doesn't have to do with your code itself, it has to do with your call to the API. You need to check the documentation to see how you get back more than one page. I just looked at the API and it says you have to make several calls to get more than 100 entries.
pagination Presentation of data is done with pagination, and it will appear max 100 records at a time display that listing. To view the next page uses one parameter page where the first page is page = 1 (1-indexed). Example:
http://hotell.difi.no/api/json/fmsf/partistotte/2011?page=8
So it appears that the page=NUM is what you need to be doing. You need to have some kind of loop that changes that page number to get more information from different pages. The rest of your code should be fine.
hb8
5,660 PointsThanks for the help Erik !