XMLHttpRequest对象

创建XMLHttpRequest对象,对于不同的浏览器,有不同的方法

对于IE:
              var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
对于For Mozilla, Firefox, Safari, and Netscape:
             var xmlhttp=new XMLHttpRequest()
XMLHttpRequest对象引用
方法

方法

描述

abort
取消当前请求
getAllResponseHeaders()
获取完整的Http header信息
getResponseHeader(headername)
获取指定的Http header信息
open(method,url,async,user,passwd)
打开一个请求。
method-指定请求方法get或post
url-请求的url
async-指定异步请求响应。true表示发送请求后不等待回应而去执行别的操作;false表示请求后等待回应后才去继续别的操作。
user-访问用户(可选)
passwd-访问密码(可选)
send(content)
发送请求
setRequestHeader("label","value")
设置请求头部信息
属性

属性名

描述

onreadystatechange
一个事件,用来捕获所有的状态变换
readyState
返回对象状态:
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
responseText
响应文本
responseXML
响应XML数据
status
返回状态数字(如:” 404”表示“Not Found” “200”表示“OK”)。
statusText
返回状态文本(如:“Not Found” ,“OK”)

例子

var Check = createAjaxObj();//创建XmlHttpRequest对象

function createAjaxObj(){
     var httprequest=false
      if(window.XMLHttpRequest){
          httprequest= new XMLHttpRequest()
      if(httprequest.overrideMimeType)
          httprequest.overrideMimeType('text/xml')
      }
      else if(window.ActiveXObject){
               try{
                     httprequest= new ActiveXObject("Msxml2.XMLHTTP");
               }
                catch(e){
                     try{
                        httprequest=new ActiveXObject("Microsoft.XMLHTTP");
                     }
                     catch(e){            
                     }
           }
        
     }
  return httprequest

function CheckAcount(){     
    if(Check.readyState==4||Check.readyState==0){
         var str=document.forms["registerForm"].elements["userId"].value;     
         Check.open("get",'User.do?userId='+str+'&method=CheckAcount');//发送请求
         Check.onreadystatechange=handleCheck;     //处理返回数据函数为handleCheck
         Check.send(null);
       }   
}


function handleCheck(){
    if(Check.readyState==4){
    var s=Check.responseText;   
      if(s==0){
           document.getElementById("sp_userId").className='userInfoYES';
      }
      else  document.getElementById("sp_userId").className='userInfoNO';
     
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值