gpt4 book ai didi

ajax - 寻找一个极其简单的AJAX脚本

转载 作者:行者123 更新时间:2023-12-01 16:19:33 25 4
gpt4 key购买 nike

我需要一个非常基本、简单且轻量级的 AJAX 脚本。

有人有这样的脚本的 shell 可以分享吗?

这是我所拥有的:

  • 我的服务器上有一个 PHP 脚本,它会回显服务器上的当前日期和时间
  • 我只需要调用 php 脚本并将回显文本字符串加载到 js var 中的 javascript,以便我可以在我的应用程序中使用它

(我需要服务器时钟的原因是该网站的所有访问者都必须在同一时钟下工作。该应用程序不适用于服务器时区之外的访问者。)

感谢您的帮助。

最佳答案

JQuery 或许是 AJAX 的正确答案,但您也可以使用普通的旧 Javascript 来执行此操作,如下所示:

 <html>
<head>
<script type="text/javascript">
function loadXMLDoc(){
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

//the callback function to be callled when AJAX request comes back
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("POST","<<url of web address>>",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>

</body>
</html>

关于ajax - 寻找一个极其简单的AJAX脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5952353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com