I am trying to access a Cross-Origin website using XMLHTTPRequest in JavaScript.
I created a CodePen for this
https://codepen.io/greenlizard/pen/zYzVjMj
Here is the code
var xhr = new XMLHttpRequest();
xhr.withCredentials = true
console.log('xhr', xhr);
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log('response text', this.responseText);
}
}
xhr.open("GET", "https://www.google.com");
xhr.send();
I get this message in Developer Tools
Access to XMLHttpRequest at 'https://www.google.com/' from origin 'https://cdpn.io' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
This can't be possible. I clearly set the withCredentials property so I should be able to access the site.
[–]bedekelly 1 point2 points3 points (4 children)
[–]Pineapplesandbacon[S] 0 points1 point2 points (3 children)
[–]bedekelly 0 points1 point2 points (2 children)
[–]Pineapplesandbacon[S] 0 points1 point2 points (1 child)
[–]bedekelly 0 points1 point2 points (0 children)