4 回答

TA貢獻1856條經驗 獲得超11個贊
基于XMLHttpRequest的文檔:
function returnStatus(req, status) {
//console.log(req);
if(status == 200) {
console.log("The url is available");
// send an event
}
else {
console.log("The url returned status code " + status);
// send a different event
}
}
function fetchStatus(address) {
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
// in case of network errors this might not give reliable results
if(this.readyState == 4)
returnStatus(this, this.status);
}
client.open("HEAD", address);
client.send();
}
fetchStatus("/");
但是,這僅適用于與當前URL位于同一域中的URL。您想要能夠ping外部服務嗎?如果是這樣,您可以在服務器上創建一個簡單的腳本,為您完成工作,并使用javascript來調用它。
添加回答
舉報