如何使用javascript获取服务器时间
当用户在如何使用javascript获取服务器时间,javascript,xmlhttprequest,Javascript,Xmlhttprequest,当用户在textboxtonothertextbox中键入某个内容以获取服务器时间时,我正在更改我的问题此代码中缺少什么或有什么错误。我对编程非常缺乏经验。你能帮我一下吗 protected void Page_Load(object sender, EventArgs e) { Response.Expires = -1; Response.Write(DateTime.Now.ToShortTimeString()); Response.End(); }
textboxt
onothertextbox
中键入某个内容以获取服务器时间时,我正在更改我的问题此代码中缺少什么或有什么错误。我对编程非常缺乏经验。你能帮我一下吗
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>
</p>
</body>
</html>
受保护的无效页面加载(对象发送方,事件参数e)
{
Expires=-1;
Write(DateTime.Now.ToShortTimeString());
Response.End();
}
函数ajaxFunction(){
var-xmlHttp;
xmlHttp=新的XMLHttpRequest();
xmlHttp.onreadystatechange=函数(){
if(xmlHttp.readyState==4){
document.getElementById(“用户”).value=xmlHttp.responseText;
}
否则{
document.getElementById(“标签”).innerHTML=“等待”;
}
open(“GET”,“Default.aspx”,true);
xmlHttp.send(空);
}
}
您将调用放错了位置以实际启动请求
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);
}