简单的Ajax例子

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
            <title>ajaxѧϰ</title>
            <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
            <script type="text/javascript">
                var xmlHttp;
                function createXMLHttpRequest(){
                    if(window.XMLHttpRequest){
                        //for IE7+,Firefox,Chrome,Opera,Safari
                        xmlHttp = new XMLHttpRequest();
                    }else{
                        //for IE6,IE5
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    
                    }
                }
                function start0Request(){
                    createXMLHttpRequest();
                    //open(method,url,async)
                    //规定请求的类型、URL 以及是否异步处理请求。
                    //method:请求的类型;GET 或 POST url:文件在服务器上的位置 async:true(异步)或 false(同步)
                    xmlHttp.open("get","http://m.weather.com.cn/data/101100101.html",false);
                    xmlHttp.send();
                    var result = xmlHttp.responseText;
                    alert(result);
                    document.getElementById("weatherID").innerHTML = "";
                    document.getElementById("weatherID").innerHTML = result ;
                }
                function start1Request(){
                    createXMLHttpRequest();
                    xmlHttp.open("get","http://m.weather.com.cn/data/101100101.html",true);
                    document.getElementById("weatherID").innerHTML = "";
                    //onreadystatechange    存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。
                    //readyState    存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。
                    //0: 请求未初始化    1: 服务器连接已建立    2: 请求已接收    3: 请求处理中    4: 请求已完成,且响应已就绪
                    //status    200: "OK"    404: 未找到页面
                    xmlHttp.onreadystatechange = function(){
                        if(4 == xmlHttp.readyState && 200 == xmlHttp.status){
                            var result = xmlHttp.responseText;
                            document.getElementById("weatherID").innerHTML = result ;
                        }    
                    }
                    
                    xmlHttp.send();
                }
                
            </script>
    </head>
    <body>
        <input type="button" value="ajax同步" onclick="start0Request()"/>
        <input type="button" value="ajax异步" onclick="start1Request()"/>
        <div id="weatherID"></div>
    </body>
</html>

简单的使用Ajax,多多指教!谢谢!

转载于:https://www.cnblogs.com/xiaoxian1369/p/3255017.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值