$.getJSON not working on this pen

When you make a request from a domain to another, you basically have to tell where you are coming from. The “*” in origin, is used to make anonymous requests and allow access from anywhere (which in your case, is codepen).

As to where to insert it, you just need to add it in your URL like any other paramater. In fact, you don’t even need the urlEnding var; you can add &format=json where you prefer; order isn’t important:

"https://en.wikipedia.org/w/api.php?origin=*&action=query&format=json&list=search&srsearch="

Edit: If you want to keep things neater, you could even use an object for your paramaters:

var wikiSearchURL = "https://en.wikipedia.org/w/api.php";
var queryParams = {
  action: 'query',
  format: 'json',
  origin: '*',
  list: 'search',
  srsearch: ''
};

Then you modify the srsearch property with the user input and pass the updated object to the $.getJSON method:

    queryParams.srsearch = $("#search").val();
    $.getJSON(wikiSearchURL, queryParams, function(data) {
      console.log(data);
    });
1 Like