利用dom方式进行异步传输,XMLHttpRequest对象的创建,数据的异步交互

jquery的区别是:jquery封装了XMLHttpRequest对象的get请求。$.get(),  data就是接受到的数据

 

Verufy.js

var xmlhttp;

//创建不同浏览器的xmlhttprequest对象

    function createXMLHttpRequest(){

        //针对fiefor,mozillar,opera,ie7,ie8

        if(window.XMLHttpRequest()){

            xmlhttp = new XMLHttpRequest();

           

        }else if(window.ActiveXObject()){

             //针对ie5,ie6

            //两个可以用于创建xmlhttprequest对象的控件名称,保存在一个js数组中

            //排在前面的版本较新

            var activexName = ["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];

            for(var i=0;i<activeName.length;i++){

                try{

                    //取出一个控件进行创建,创建成功就返回

                    xmlhttp = new ActiveXObject(activexName[i]);

                    break;

                }catch(e){

 

                }

            }

        }

 }

 

    function verify(){

        createXMLHttpRequest();

        var userName = document.getElementById("name").value;

        var url = "AjaxServer?name=" + userName + "&timestampt=" + new Date().getTime();

        //提交的方式,提交的地址,同步还是异步

       xmlhttp.open("GET",url,true);

        xmlhttp.onreadystatechange = callback;

        xmlhttp.send(null);

 

     

 //post方式提交

        var url = "AjaxXMLServer";

        xmlhttp.open("post",url,true);

        //设置头信息

        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        xmlhttp.onreadystatechange = callback;

        //异步方式。send这句话会立即执行

        xmlhttp.send("name=" + userName + "&timestampt=" + new Date().getTime());

 

    }

 

    function callback(){

        if(xmlhttp.readyState == 4){   //全部传完了

            if(xmlhttp.status == 200){ //传递的消息正确

                var resTextdiv = document.getElementById("result");

                resTextdiv.innerHTML = "<font color='red'>" + xmlhttp.responseText + "</font>";

            }

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值