正如標題所說,我想要得到的響應頭日期的價值,但我不斷收到以下警告:獲取日期標題Asyncronously
在主線程同步的XMLHttpRequest已被棄用,因爲 其不利影響以最終用戶的體驗。如需更多幫助,請撥打 查詢https://xhr.spec.whatwg.org/。
我的代碼:
function getxmlhttp() {
// although IE supports the XMLHttpRequest object, but it does not work on local files.
var forceActiveX = (window.ActiveXObject && location.protocol === "file:");
if (window.XMLHttpRequest && !forceActiveX) {
return new XMLHttpRequest();
}else {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
alert ("Your browser doesn't support XML handling!");
return null;
};
function srvTime(){
xmlHttp = getxmlhttp();
//xmlHttp.open('HEAD',window.location.href.toString(),false);
//need to send this to a non-volitile page
xmlHttp.open('GET',"blank.php",false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send(null);
console.log("raw " + xmlHttp.getResponseHeader("Date"));
return xmlHttp.getResponseHeader("Date");
};
當我轉這行:
xmlHttp.open('GET',"blank.php",true);
是真實的,值返回NULL
。
所以可以這樣做,還是我必須在控制檯中生存警告?
謝謝
是jQuery的選項嗎?如果是,那麼看到這個答案。 http://stackoverflow.com/a/1457708/1437261 – Gogol
你是否將腳本標記中包含的腳本從blank.php返回給客戶端? – dreamweiver
您需要使用帶有異步請求的onreadystatechange/load處理程序,然後才能使用數據/頭文件。見https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Get_last_modified_date –