I know this error has been posted on this site before, and I've been able to resolve the error, but I'm looking for some explanation on what I've actually done.
So as a learning project I am creating a Wikipedia Viewer, and the first thing I tried to do was to make a call to the Wikipedia API:
$(document).ready(function() {
var wiki = "https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json"
$.getJSON(wiki, function(d) {
console.log(d);
});
});
This returns an error of XMLHttpRequest cannot load https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://conn3cted.uk.tn' is therefore not allowed access.
I was able to stop this error appearing by adding &callback=?
onto the end of the GET request (https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json&callback=?
)
But I don't fully understand what callback=?
is doing, which would stop it erroring?
Edit: This is different to the question linked, as I'm not asking about JSONP (I don't think?) and that questions is centered about using <script>
tags, whereas I am asking why adding callback=?
made this work.