I´m having some problems with the challenge "wikipedia viewer"

hi, i’m in the challenge “Wikipedia viewer” but i have a problem with the API. actually i use this API " https://en.wikipedia.org/w/api.php?action=opensearch&search="+ term +"&format=json&callback=?" and not return nothing.
Can someone help me to observer mi code and tell me what is wrong?

$("#search").click(function(){
  var term = $("#toSearch").val();
  var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+ term +"&format=json&callback=?";
  $.ajax({
    url: url,
    type: "GET",
    async: false,
    dataType: "json",
    success: function(data, status, jqXHR){
    console.log(data);
  },
    error: function(request,error){
      console.log (arguments);
      alert ("error "+ error);
    }
  })
 })

Do you have this in CodePen? Could you link to your project?

I’ve seen this error crop up a lot recently. I think it’s the way everyone is using .ajax. I find the quickest fix for this is to remove the callback from your url (which does work because I just copy/pasted it into a new browser tab and it returns data) and change dataType to “jsonp”.

Doing that will let jQuery do its thing. It will add the callback for you and remove it before shipping to the success method.

So, this:

$("#search").click(function(){
  var term = $("#toSearch").val();

  // remove callback from url
  var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+ term +"&format=json";
  $.ajax({
    url: url,
    type: "GET",
    async: false,
    dataType: "jsonp", // change to `jsonp` here
    success: function(data, status, jqXHR){
    console.log(data);
  },
    error: function(request,error){
      console.log (arguments);
      alert ("error "+ error);
    }
  })
 })

Give it a try.

2 Likes

hi, i’m sorry for late to answer. finally i can solve it, the wrong was to use a form and remove callback from url. thanks a lot for your answers.

i´m sorry, my english is poor but i´m studying it

2 Likes

The callback argument in Wikipedia’s API is confusing.

1 Like