【发布时间】:2015-06-07 16:37:02
【问题描述】:
我正在尝试检查 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>
我收到错误消息: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 相关的代码。
请告诉我如何解决这个问题。
【问题讨论】:
-
XMLHttpRequest是 AJAX。这就是 jQuery 核心$.ajax的动力。
标签: javascript