1

I am trying to make a JSON request using jquery, the first JSON request return me the data I want.

After the first Success JSON request, I want to look at the results from the first JSON request and use it in my second JSON request.

The problem I am facing currently, is my second JSON request return empty object. Can someone help me figure out why my second JSON return nothing?

Even though when i post the second JSON url on a website url, it show me the JSON data. but in my program it is not returning anything.

Here is the code

$(document).ready(function() {

  var api_wiki = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=cat&srwhat=text&callback=?";
  var api_wiki_extract = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=info%7Cextracts&inprop=url&exsentences=1&callback=?&titles=";
  var sp;
  var linkReady = [];
  var linkR;

  $.getJSON(api_wiki, function(data) {
    sp = data.query.search;
    linkR = workWithTheData(sp);
    anotherJSON(linkR);
  }); //End of getJSON 1

  function workWithTheData(sp) {
    for (var i = 0; i < sp.length; i++) {
      linkReady[i] = api_wiki_extract + sp[i].title;
    }
    return linkReady;
  }

  function anotherJSON(linkR){
    $.getJSON(linkR[0], function(data1) {
     console.log(data1); 
    });
  }

}); //End of document ready

1 Answers1

1

A few things you need to understand here:

  1. JSON is a data format. What you are doing is HTTP Ajax Request, provided by jQuery, which return data in JSON format.

  2. Your code is totally working fine. you can see @Hanlet 's link.

After your second Request, it will return {batchcomplete: "", query: Object} in Browser's Console. And once you expand the query, you will see everything inside.

  1. The 2nd scenario that you might facing, is, may be there are some other parts affect your ajax request.

You can try to to convert back to normal $.ajax request and set async: false Is it possible to set async:false to $.getJSON call

Community
  • 1
  • 1
Jacky
  • 2,924
  • 3
  • 22
  • 34