我正在尝试检查 URL 是否存在。但我被 CORS 抓住了 - 这就是我尝试过的:
<!DOCTYPE html>
<html>
<head>
<script>
function checkUrl(url){
var request = new XMLHttpRequest;
request.open('GET', url, true);
request.send();
request.onreadystatechange = function(){
if(request.readyState==4){
if(request.status==200){
console.log(request.readyState);
return true;
}
}else{
return false;
}
}
};
var url = 'http://localhost:' + 8080 + '/hello/';
if(checkUrl(url)){
alert('Success');
}else{
alert('Failure');
}
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我收到错误:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/hello/. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
注意:我有一个限制:我无法使用任何 Ajax/jQuery 相关代码。
请让我知道如何解决这个问题。