0

var url = 'http://en.wikipedia.org/w/api.php?action=opensearch&format=json&limit=15&search=' + search;
var ourRequest = new XMLHttpRequest();

ourRequest.open('GET', url);

ourRequest.onload = function() {
  var data = JSON.parse(ourRequest.responseText);
  console.log(data);
};
ourRequest.send();

Can someone tell me why i am not able to get parsed data in my console.

codtex
  • 6,128
  • 2
  • 17
  • 34
Luffy_9
  • 13
  • 4

2 Answers2

0

Well i made it to work with the following code.

$.ajax({
url: url,
jsonp: "callback",
dataType: "jsonp",
success: function(data) {
  console.log(data);
 }
 });
Luffy_9
  • 13
  • 4
0

You need to add one more parameter to your url variable to make this request work - origin=*. Add it and your code will be fine.

Check how I've changed the url variable. Wikipedia API requires the request origin included in the parameters string.

document.getElementById('do-search').addEventListener('click', search);

function search(){
  var search = document.getElementById('search').value;
  var url='http://en.wikipedia.org/w/api.php?action=opensearch&format=json&limit=15&origin=*&search='+search;
  var ourRequest = new XMLHttpRequest();

  ourRequest.open('GET', url);

  ourRequest.onload = function() {
    var data = JSON.parse(ourRequest.responseText);
    document.getElementById('results').innerHTML = data;
  };
  ourRequest.send();
}
<input id="search" type="text">

<button id="do-search">Search</button>

<div id="results"></div>
codtex
  • 6,128
  • 2
  • 17
  • 34