I am trying this code but it gives me a DOM Exception. What I want it to get a true/false "answer" from the function using plain Javascript.
我正在尝试这个代码,但它给了我一个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);
小提琴
I got this code from this SO answer, but as said I cannot get it working...
我从这个SO答案中得到了这个代码,但正如我说的那样,我无法让它工作......
1 个解决方案
#1
0
Cross domain does not give any status code. status code is part of the content which is received from server when server responds with yes or no. In case of cross domain server never respond to request.
跨域不提供任何状态代码。当服务器以是或否响应时,状态代码是从服务器接收的内容的一部分。如果跨域服务器永远不响应请求。
the second mistake in the code is you cannot capture the status code directly without any waiting time or success event. return statement in function doesn't wait until server response to ajax request, so you cannot depend on it.
代码中的第二个错误是您无法在没有任何等待时间或成功事件的情况下直接捕获状态代码。函数中的return语句不会等到服务器响应ajax请求,所以你不能依赖它。
#1
0
Cross domain does not give any status code. status code is part of the content which is received from server when server responds with yes or no. In case of cross domain server never respond to request.
跨域不提供任何状态代码。当服务器以是或否响应时,状态代码是从服务器接收的内容的一部分。如果跨域服务器永远不响应请求。
the second mistake in the code is you cannot capture the status code directly without any waiting time or success event. return statement in function doesn't wait until server response to ajax request, so you cannot depend on it.
代码中的第二个错误是您无法在没有任何等待时间或成功事件的情况下直接捕获状态代码。函数中的return语句不会等到服务器响应ajax请求,所以你不能依赖它。