Getting data from jsonp api containing pages

I got a little stuck on how I can get all the data from the jsonp api when the data is paginated. The callback is as follows:

{
entries: [],
page: 1,
pages: 101,
posts: 10054
}

only the code only gets the results from page 1, but I would like to get the results from all 101 pages. If I add a request to a URL, for example: &page=2or &page=3, I can access objects only from this page, I try to access all objects from all pages at a time .... I would appreciate 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);

})
+4
source share
2 answers

, , .

$.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);
    });
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
</div>
Hide result
+2

jsonp, $.ajax(), $.getJSON(). javascript-.

$("#btn").click(function(){
$.ajax({
        url: "http://hotell.difi.no/api/jsonp/mattilsynet/smilefjes/tilsyn?callback=myJsonpCallbackMethod",
        dataType: "jsonp",
        success: function( response ) {
            //console.log( response ); // server response
        }
    });
});

function myJsonpCallbackMethod(data){
  alert("Hello");
  console.log( data ); // server response
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button id="btn">Click Me</button>
Hide result
0

Source: https://habr.com/ru/post/1653303/


All Articles