Wikipedia Viewer - trouble understanding request URL

Wikipedia Viewer - trouble understanding request URL
0

#1

I’m struggling a bit to wrap my head around this one. Initially I was trying the following URL to retrieve data from the Wikipedia api:

https://en.wikipedia.org//w/api.php?action=query&format=json&list=search&utf8=1&srsearch=searchTermHere

This seemed to work when I pasted it in my browser, I saw many related results pop up. However When I tried to access it with my ajax get request I was getting errors. I searched around and found someone who suggested using this url:

https://en.wikipedia.org/w/api.php?action=opensearch&search= + searchTermHere + &format=json&callback=?;

I did and viola, it works! But I don’t understand why. Before I copy and paste I want to understand why my original url was not returning information while this one is. I mean they seem to provide roughly the same data when I enter them in my browser so I’m just not getting where the difference lies. Any pointers?


#2

#3

There’s a double backslash in the top one that is a typo. Assuming that isn’t the issue is not that typo, the callback=? part of the second one is what asks for a JSONP object instead of a regular JSON one.

Jenovs’ link should explain the rest, but feel free to ask for more detail if you need it :slight_smile:


#4

Ah, good catch, I missed that typo.

So adding callback=? requests a JSONP object and then I used an ajax to get the information like so:

$.ajax({
    type:"GET",
    url: searchApi,
    async: false,
    dataType: "json",
    success: function(val) {

Couple other questions:

What I’m wondering now is should dataType be changed from json to jsonp? Is the callback=? all that’s needed to change this to a get jsonp datatype? Could you potentially use getJSON instead of ajax with the jsonp data? Is there any advantage/disadvantage to doing so?


#5

That’s the weird thing (IMO broken) about the Wikipedia API.

Even though you are requesting JSONP (as indicated by callback=?), the dataType is still JSON.

It’s messed up!

I just used $getJSON.


#6

In the $.ajax request, using dataType: 'jsonp' worked for my project.

The action=opensearch url points to a different search module. See:https://www.mediawiki.org/wiki/API:Search_and_discovery#Search_modules

It appears the action=opensearch module recognizes callback=?, but the action=query&list=search does not.


#7

Thank you for your input :slight_smile: