[英]JS redirect user based on a remote files existence
I'm trying to redirect a user based on the existence of a remote file.我正在尝试根据远程文件的存在重定向用户。 I'm not sure if this is even possible.
我不确定这是否可能。
The code in website.com: website.com 中的代码:
var result = doesFileExist("http://remote-website.com/verifier.php");
if (result == true) {
header("Location: https://website.com/");
} else {
header("Location: https://website.com/error.php");
}
I see this error on the console:我在控制台上看到这个错误:
Uncaught ReferenceError: doesFileExist is not defined
未捕获的 ReferenceError:doesFileExist 未定义
One possible way would be to send a GET request to the page, and check if the response code is 404 (not found):一种可能的方法是向页面发送 GET 请求,并检查响应代码是否为 404(未找到):
fetch(url)
.then(function(response) {
var status = response.status; //the response code, e.g. 200, 404, ...
if(status == 404) {
//do something
}
});
Note that some websites automatically redirect you to a default error page if the requested file does not exist, and in that case the response code you get may not be a 404.请注意,如果请求的文件不存在,某些网站会自动将您重定向到默认错误页面,在这种情况下,您获得的响应代码可能不是 404。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.