What is the purpose of the callback in the wikipedia API URL?

Here is the API URL : https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=tesla&callback=JSON_CALLBACK

2 Likes

That is a jsonp request described here

https://www.mediawiki.org/wiki/API:Cross-site_requests

jsonp requests are made via a <script> tag in the HTML page which is a bit of a pain - JSON_CALLBACK is the name of your callback function to process the data returned by the <script> tag - something like

function dojsonp(data) {
console.log(JSON.stringify(data))
}

the <script> tag can be static or dynamic


The wiki doc above mentions the alternative cors request using an origin=* parameter that can be made with XMLHTTPRequest - I had to urlencode “*” below as “%2a” to prevent the url being modified by this forum posting software but you don’t have to do that

https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=tesla&origin=%2a"

1 Like

Thanks very much.
I will be honest with you, its not crystal clear to me but still I have learned something here.

These are good references for web APIs - they are not easy to grok - use them more as goalposts to get you started kicking the ball

https://developer.mozilla.org/en-US/docs/AJAX

You may get more help on this forum if you tell the forum what you’re trying to do and the issues you face

1 Like

To be honest I don’t know, I’m going to try and figure it out at some point, I removed from the URL and my script worked fine.

That was not the case with me, for me after removing the part I didn’t get any JSON.