寻找一个非常简单的AJAX脚本 [英] Looking for an extremely simple AJAX script

查看:58
本文介绍了寻找一个非常简单的AJAX脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个非常基本,简单且轻巧的AJAX脚本.

I need a very basic, simple and lightweight AJAX script.

有人拥有可以共享的这种脚本的外壳吗?

Does anyone have a shell of such a script they can share?

这就是我所拥有的:

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

(我需要服务器时钟的原因是网站的所有访问者都必须使用相同的时钟.该应用程序不适用于服务器所在时区以外的访问者.)

(the reason I need the server's clock is that all visitors to the site have to work off the same clock. The app does not work for visitors outside the server's timezone.)

感谢您的帮助.

推荐答案

JQuery也许是AJAX的正确答案,但是您也可以使用普通的旧Javascript来做到这一点,如下所示:

JQuery is perhaps the right answer for AJAX but you can also do this in plain old Javascript as follows:

 <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脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆