Problem with Wikipedia viewer/API

Problem with Wikipedia viewer/API
0.0 0

#1

Hi there!

I’m having an issue with the Wikipedia Viewer challenge, namely getting hold of the JSON for the search results.

I’ve tried using the $.ajax and $.getJSON functions, and neither is working for me.

$(“document”).ready(function(){

//Submit button action
$("#submit-btn").on(“click”, function() {
var input = $("#text-search").val();
var searchURL = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exsentences=1&generator=search&gsrsearch="+input+"&gsrlimit=10&callback=?";

/*
//Ajax attempt
$.ajax({
type: “GET”,
url: searchURL,
dataType: “json”,
success: function (data) {
console.log(data);
},
error: function(errorMsg) {
console.log(“Error”);
}
});
*/

//getJson attempt
$.getJSON(searchURL, function (data) {
console.log(data);
});

});
});

The URL I generate works when I test it in my browser, but in Codepen the following is returned in the console:

Object {
continue: Object {
continue:"||",
excontinue: 1
},
query: Object {
pages: Object {}
}
}

Does anyone know why I’m not getting the proper response in codepen? I thought I’d worked around the json/jsonp problem, but clearly there’s still some issue.


#2

I used this form in my wikipedia viewer

$.ajax({
      url: "https://en.wikipedia.org/w/api.php",
      dataType: "json",
      data: {
        action: "opensearch",
        format: "json",
        origin: "*",
        search: searchTerm,
        formatversion: "2"
      }
})

Maybe you can tweak it a bit, since you’re using query instead of opensearch. But I don’t know how it will turn out in codepen, since mine is using gh pages.


#3

Can you please link to your CodePen? That will make it easier to provide feedback.


#4

@PortableStick Sure! Here: http://codepen.io/Nossorgs/pen/pegYQX?editors=1111

@kevcomedia I did try opensearch and it worked (thanks!), though I’d like to know what’s wrong with the generator version as well.


#5

I can’t help you with that :slight_frown:. I couldn’t manage to make action=query to work. However, once you are done with the wikipedia viewer (or just plain stuck) try taking a look at how other campers did theirs (or at least, what request URL they used).


#6

I’ll give that a go for sure - the Wikipedia API is pretty tricky!

In the meantime I’ve set up a version that uses opensearch. Still need to figure out the finer points of using css, but the information seems to be displayed properly: