Trying to receive a response from the Wikipedia API on Codepen. The reply should be a json which i'm trying to console.log.
In the console however I see an error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://en.wikipedia.org/w/api.php?action=opensearch&search=earth&format=json. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
I've read quite a bit over the past few days about CORS and Allow-Origin to try to understand but for some reason even when I think I understand... I cannot manage to implement :)
However, the most interesting thing is this - even though console shows such an error message if I look at the actual response in developer tools Network tab, I see the json response in all its glory!
It would be great to have an explanation how is that possible?
Codepen link here
var xhrObject = new XMLHttpRequest();
xhrObject.onreadystatechange = function() {
if (xhrObject.readyState === 4 && xhrObject.status === 200) {
console.log(xhrObject.responseText);
}
};
xhrObject.open(
"POST", "https://en.wikipedia.org/w/api.php?action=opensearch&search=earth&format=json", true
);
xhrObject.send();
Thanks in advance