kurumkan
kurumkan

Reputation: 2725

Ajax request to Wikipedia API issue

I want to make a simple request to the Wikipedia API:

$.ajax({
    url: 'http://en.wikipedia.org/w/api.php',
    data: {
        action: "query",
        generator: "search",
        gsrnamespace: 0,
        gsrsearch: "te",
        gsrlimit: 30,
        prop: "info|extracts",
        inprop: "url",
        format: "json"
    },
    dataType: 'jsonp',
    success: processResult
});

The outcome should look like this -> https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrnamespace=0&gsrsearch=te&gsrlimit=10&prop=info|extracts&inprop=url

But this doesn't work properly from my ajax code.(there must be "extract" key).

I think the problem is in simbol "|" (look at ajax):

        prop:"info|extracts",

How to fix?

Upvotes: 0

Views: 885

Answers (2)

Tgr
Tgr

Reputation: 28160

$.ajax({
    url: 'https://en.wikipedia.org/w/api.php',
    data: {
        action: 'query',
        generator: 'search',
        gsrnamespace: 0,
        gsrsearch: 'te',
        gsrlimit: 30,
        prop: 'info|extracts',
        inprop: 'url',
        format: 'json',
        origin: '*'
    }
}).done( processResult );
  • uses plain AJAX instead of JSONP
  • uses origin: '*' (which is required for that)
  • uses HTTPS instead of HTTP (which won't work)

Not sure if this solves your problem because it is not very clear what your problem is...

Upvotes: 1

kurumkan
kurumkan

Reputation: 2725

1.You can use getJSON instead of $.ajax

$.getJSON("https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrnamespace=0&gsrsearch=te&gsrlimit=10&prop=info|extracts&inprop=url&format=json&callback=?", processResult);

2.Remember to add parameter with value(to avoid jQuery AJAX cross domain error)

callback=?

3.extracts are not always available

Upvotes: 0

Related Questions