簡體   English   中英

JS基於遠程文件存在重定向用戶

[英]JS redirect user based on a remote files existence

我正在嘗試根據遠程文件的存在重定向用戶。 我不確定這是否可能。

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"); 
}

我在控制台上看到這個錯誤:

未捕獲的 ReferenceError:doesFileExist 未定義

一種可能的方法是向頁面發送 GET 請求,並檢查響應代碼是否為 404(未找到):

fetch(url)
.then(function(response) {
  var status = response.status; //the response code, e.g. 200, 404, ...
  if(status == 404) {
    //do something
  }
});

請注意,如果請求的文件不存在,某些網站會自動將您重定向到默認錯誤頁面,在這種情況下,您獲得的響應代碼可能不是 404。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM