ajax使用。

<script>
    function createAjax(){
        var request=false;
        
        //window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE8)
        if(window.XMLHttpRequest){
            request=new XMLHttpRequest();

            if(request.overrideMimeType){
                request.overrideMimeType("text/xml");
            }
        

        //window对象中有ActiveXObject属性存在就是IE
        }else if(window.ActiveXObject){
            
            var versions=['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];

            for(var i=0; i<versions.length; i++){
                    try{
                        request=new ActiveXObject(versions[i]);

                        if(request){
                            return request;
                        }
                    }catch(e){
                        request=false;
                    }
            }
        }
        return request;
    }

//注意: 要每次请求都要使用一个新的XMLHttpRequest
/*
    如果使用get将数据传给服务器,则服务器就使用$_GET
    就直接通过Url将数据传给服务器

    使用POST时一定要使用    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
  */ 
var ajax=null;
function show(){
    var helloobj=document.getElementById("hello");
    ajax=createAjax();
    ajax.onreadystatechange=function(){
        if(ajax.readyState==4){
            if(ajax.status==200){
                var dom=ajax.responseXML;
                
                var users=dom.getElementsByTagName("user");

                alert(users.length);
            }else{
                alert("页面请求失败");
            }
        }
    }
    ajax.open("post", "users.xml", true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("username=lisi&email=1111111111");
}
</script>
<input type="button" onclick="show()" value="request">
<div style="width:500px;height:200px;background:yellow" id="hello">
</div>    

 

转载于:https://www.cnblogs.com/h-g-f-s123/p/5815113.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值