正如标题所说,我想要得到的响应头日期的价值,但我不断收到以下警告:获取日期标题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 –