ajax get call example

by bilbobaggins

HTML

<div id="article">

</div>
<a href="javascript:void()" id="next"  class="next">Next &raquo;</a>

<a href="#" class="previous round" id="prev" >Prev&#8249;</a>

JavaScript

var nextrequest="";
var internset="";
//San_Francisco
//https://en.wikipedia.org/w/api.php?action=query&titles=India&prop=images&imlimit=1&format=json

$(document).ready(function(){

$("#next").click(function() {
   if(nextrequest!="")
 {
 console.log(internset)
 loadresults(internset);
 }
});


$("#prev").click(function() {
   if(nextrequest!="")
 {
 console.log(internset)
 loadresults(internset);
 }
});


 loadresults("");
 
 function loadresults(dataset)
 {
 
 
    $.ajax({
        type: "GET",
        url: "https://en.wikipedia.org/w/api.php?action=query&list=allcategories&acprefix=List%20of&format=json&accontinue="+dataset+"&callback=?",
        contentType: "application/json; charset=utf-8",
        async: false,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
 //send in next request
 console.log(data)
 
 if(data.continue!=undefined)
 {
 nextrequest=data.continue.accontinue;
 internset=data.continue.accontinue.split('|')[0];
 }
 else
 {
 $("#next").hide();
 }
 

       //     console.log(data.query)
              
               var subarchive=data.query.allcategories;
               
         //       console.log(internset)
                $("#article").html("");
              Object.keys(subarchive).forEach(key => {
   // console.log(key);          // the name of the current key.
  //  console.log(subarchive[key]["*"]);   // the value of the current key.
    var currentvalset=subarchive[key]["*"];
    
    $("#article").append(currentvalset+"<br/>")
});
            
 
        },
        error: function (errorMessage) {
        }
    });
    }
});