如何使用javascript获取服务器时间

How to get server time using javascript

本文关键字:服务器 时间 获取 javascript 何使用      更新时间:2023-09-26

我正在改变我的问题当用户在textboxt或其他texbox中键入一些内容以获取服务器时间时,这段代码中缺少什么或错了什么,但什么都没发生。我对编程非常缺乏经验。你能帮吗

protected void Page_Load(object sender, EventArgs e)
{
      Response.Expires = -1;
      Response.Write(DateTime.Now.ToShortTimeString());
      Response.End();
}
 <script type="text/javascript">
  function ajaxFunction() {
    var xmlHttp;
        xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {

                document.getElementById("user").value = xmlHttp.responseText;
            }
            else {
                document.getElementById("label").innerHTML = "wait";
            }

            xmlHttp.open("GET", "Default.aspx", true);
            xmlHttp.send(null);
        }
}
 </script>
<title></title>
</head>
 <body>
   <input id="user" type="text"  onkeyup="ajaxFunction()"/>
  <p>
    <input id="time" type="text" /></p>
<div id="label">
</div>
<p>
    &nbsp;</p>
 </body>
 </html>

您将调用放错了位置,从而实际启动了请求

function ajaxFunction() {
    var xmlHttp;
    xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {
            document.getElementById("user").value = xmlHttp.responseText;
        } // closes the if
        else {
            document.getElementById("label").innerHTML = "wait";
        } // closes the else
    } // closes the function() for onreadystatechange
    // call the stuff
    xmlHttp.open("GET", "Default.aspx", true);
    xmlHttp.send(null);
}