我正在制作一个Web应用程序,要求我检查远程服务器是否在线.当我从命令行运行它时,我的页面加载速度达到了整整60秒(对于8个条目,它会随着更多线性线性扩展).
我决定在用户端进行ping操作.这样,我可以加载页面,让他们在浏览我的内容时等待"服务器在线"数据.
如果有人对上述问题有答案,或者他们知道解决方案以保持我的页面加载速度快,我肯定会感激.
我正在尝试这个代码,但它给了我一个DOM异常.我希望它使用普通的Javascript从函数中获得真/假"答案".
var url = 'http://www.google.com/';
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
UrlExists(url);
Run Code Online (Sandbox Code Playgroud)
我从这个 SO答案得到了这个代码,但正如我说的那样,我无法让它工作......
我想仅在该网页可用时才发出网页请求.我用angularjs + javascript写了我的应用程序.有没有办法确定网页是否可用使用javascript?
我正在尝试检查 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 相关代码。
请让我知道如何解决这个问题。