Skip to content

Instantly share code, notes, and snippets.

@viko16
Last active October 23, 2015 02:37
Show Gist options
  • Save viko16/9895873 to your computer and use it in GitHub Desktop.
Save viko16/9895873 to your computer and use it in GitHub Desktop.
ajax获取服务器时间 #javascript
var something = "http://www.baidu.com"; //任意同域资源,size越小越好
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", something, false);
xmlhttp.send();
console.log("服务器时间:" + new Date(xmlhttp.getResponseHeader("date"))); //自己解析响应头,不一定是"date"的
console.log("本机的时间:" + new Date());
with(new XMLHttpRequest) {
open("GET", "/");
onreadystatechange = function(){
if(readyState >= 2 && getResponseHeader('Date')){
console.log(getResponseHeader('Date'))
abort();
}
}
send();
}
@viko16
Copy link
Author

viko16 commented Mar 31, 2014

Notice

  1. 没考虑ie的情况
  2. 不能跨域
  3. post还是get自己改
  4. 自行分析响应头,时间不一定是date

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment