有没有一种方法可以在没有服务器端语言的情况下完成http头请求并获得时间

Is there a way to do a http head request and get the time without a server side language

本文关键字:http 情况下 请求 时间 语言 服务器端 一种 方法 有没有      更新时间:2023-09-26

我正在尝试从Javascript代码中执行http头请求,以获取服务器时间。。。我可以从javascript中做到这一点吗?或者我需要一种服务器端语言来实现这个

假设您的服务器发送Date头(RFC说它必须发送),那么:

$.ajax('/', {
    type: 'HEAD',
    success: function(r,status,xhr) {
        alert(xhr.getResponseHeader('Date'));
    }
});

或者不使用jQuery,不会有更多的代码,但可能会减少错误处理:

function getServerTime() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        alert(xhttp.getResponseHeader('Date'));
    }
  };
  xhttp.open("HEAD", "/", true);
  xhttp.send();
}