This is not a duplicate of "How does Access-Control-Allow-Origin header work?".
I read some Q&A like "How to send GET request in JavaScript?" but the answers do not work (for example, this).
The error is as follows:
Access to XMLHttpRequest at 'https://~' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Could you give me an example of working code?
I want to get the html of the target URL.
my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
var theUrl = "https://www.google.com";
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
function callback(text){
console.log(text);
}
httpGetAsync(theUrl, callback);
</script>
</body>
</html>