XMLHttpRequest简单总结

一、概念
XMLHttpRequest 对象用于在后台与服务器交换数据。

XMLHttpRequest 对象是能够:

  • 在不重新加载页面的情况下更新网页
  • 在页面已加载后从服务器请求数据
  • 在页面已加载后从服务器接收数据
  • 在后台向服务器发送数据

所有现代的浏览器都支持 XMLHttpRequest 对象。


二、创建
1、法1
<script language= "javascript"   type= "text/javascript" >
      var   xmlhttp;
      //   创建 XMLHTTPRequest 对象
      function   createXMLHTTPRequest()
{
      //   判断是否支持 ActiveX 控件      if(window.ActiveXObject)
   {
             //   通过实例化 ActiveXObject 的一个新实例来创建 XMLHTTPRequest
            xmlhttp =   new   ActiveObject( "Microsoft.XMLHTTP" );
    }
  //   判断是否把 XMLHTTPRequest 实现为一个本地 javascript 对象
      else   if (window.XMLHTTPRequest)
   {
        //   创建 XMLHTTPRequest 的一个实例(本地 javascript 对象)
        xmlhttp =   new   XMLHTTPRequest();
    }
}
</script>

2、法2

function createRequest() {
    try   {
    request =   new   XMLHttpRequest();
  }   catch   (tryMS) {
      try   {
      request =   new   ActiveXObject( "Msxml2.XMLHTTP" );
    }   catch   (otherMS) {
        try   {
        request =   new   ActiveXObject( "Microsoft.XMLHTTP" );
      }   catch   (failed) {
        request =   null ;
      }
    }
  }       
    return   request;
}

三、常用属性和方法
1、 方法: open
        创建一个新的 http 请求,并指定此请求的方法、 URL 以及验证信息
      语法: oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
      //   参数
      // bstrMethod
      // http 方法,例如: POST GET PUT PROPFIND 。大小写不敏感。
      // bstrUrl
      //   请求的 URL 地址,可以为绝对地址也可以为相对地址。
      // varAsync[ 可选 ]
      //   布尔型,指定此请求是否为异步方式,默认为 true 。如果为真,当状态改变时会调用 onreadystatechange 属性指定的回调函数。
      // bstrUser[ 可选 ]
      //   如果服务器需要验证,此处指定用户名,如果未指定,当服务器需要验证时,会弹出验证窗口。
      // bstrPassword[ 可选 ]
      //   验证信息中的密码部分,如果用户名为空,则此值将被忽略。
    
      //   备注:调用此方法后,可以调用 send 方法向服务器发送数据。
    xmlhttp.Open( "get" ,   "http://localhost/example.htm" ,   true );
      // var book = xmlhttp.responseXML.selectSingleNode("//book[@id='bk101']");
      // alert(book.xml);

2、 属性: onreadystatechange
      // onreadystatechange :指定当 readyState 属性改变时的事件处理句柄
      //   语法: oXMLHttpRequest.onreadystatechange = funcMyHandler;
      //   如下的例子演示当 XMLHTTPRequest 对象的 readyState 属性改变时调用 HandleStateChange 函数,
      //   当数据接收完毕后( readystate == 4 )此页面上的一个按钮将被激活
      //   备注:此属性只写,为 W3C 文档对象模型的扩展
    xmlhttp.onreadystatechange= HandleStateChange;

3、 方法: send
  //   发送请求到 http 服务器并接收回应
      //   语法: oXMLHttpRequest.send(varBody);
      //   参数: varBody   (欲通过此请求发送的数据。)
      //   备注:此方法的同步或异步方式取决于 open 方法中的 bAsync 参数,如果 bAsync == False ,此方法将会等待请求完成或者超时时才会返回,如果 bAsync == True ,此方法将立即返回。
      // This method takes one optional parameter, which is the requestBody to use. The acceptable VARIANT input types are BSTR, SAFEARRAY of UI1 (unsigned bytes), IDispatch to an XML Document Object Model (DOM) object, and IStream *. You can use only chunked encoding (for sending) when sending IStream * input types. The component automatically sets the Content-Length header for all but IStream * input types.
      //   如果发送的数据为 BSTR ,则回应被编码为 utf-8,   必须在适当位置设置一个包含 charset 的文档类型头。
      // If the input type is a SAFEARRAY of UI1, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
      //   如果发送的数据为 XML DOM object ,则回应将被编码为在 xml 文档中声明的编码,如果在 xml 文档中没有声明编码,则使用默认的 UTF-8
      // If the input type is an IStream *, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
    xmlhttp.Send(xmldoc);

4、属性 readyState
    // 返回XMLHTTP请求的当前状态
    // 语法:lValue = oXMLHttpRequest.readyState;
    // 备注:变量,此属性只读,状态用长度为的整型表示定义如下:
    // 0 (未初始化) 对象已建立,但是尚未初始化(尚未调用open方法)
    // 1 (初始化) 对象已建立,尚未调用send方法
    // 2 (发送数据) send方法已调用,但是当前的状态及http头未知
    // 3 (数据传送中) 已接收部分数据,因为响应及http头不全,这时通过responseBodyresponseText获取部分数据会出现错误,
    // 4 (完成) 数据接收完毕,此时可以通过通过responseBodyresponseText获取完整的回应数据
    if (xmlhttp.readyState == 4){
       document.frmTest.myButton.disabled = false;
       
  5、 属性:responseText
         //   将响应信息作为字符串返回
         //   语法: strValue = oXMLHttpRequest.responseText;
         //   备注:变量,此属性只读,将响应信息作为字符串返回。 XMLHTTP 尝试将响应信息解码为 Unicode 字符串,
         // XMLHTTP 默认将响应数据的编码定为 UTF-8 ,如果服务器返回的数据带 BOM(byte-order mark) XMLHTTP
         //   以解码任何 UCS-2 (big or little endian) 或者 UCS-4   数据。注意,如果服务器返回的是 xml 文档,此属
         //   性并不处理 xml 文档中的编码声明。你需要使用 responseXML 来处理。        
         alert(xmlhttp.responseText);

6、     属性: responseXML
         //   将响应信息格式化为 Xml Document 对象并返回
         //   语法: var objDispatch = oXMLHttpRequest.responseXML;
         //   备注:变量,此属性只读,将响应信息格式化为 Xml Document 对象并返回。如果响应数据不是有效的 XML 文档,
         //   此属性本身不返回 XMLDOMParseError ,可以通过处理过的 DOMDocument 对象获取错误信息。
       alert( "Result = "   + xmlhttp.responseXML.xml);
7、     属性: status
         //   返回当前请求的 http 状态码
         //   语法: lValue = oXMLHttpRequest.status;
         //   返回值:长整形标准 http 状态码,定义如下:
         // Number:Description 
         // 100:Continue
           // 101:Switching protocols
           // 200:OK
           // 201:Created
           // 202:Accepted
           // 203:Non-Authoritative Information
           // 204:No Content
           // 205:Reset Content
           // 206:Partial Content
           // 300:Multiple Choices
           // 301:Moved Permanently
           // 302:Found
           // 303:See Other
           // 304:Not Modified
           // 305:Use Proxy
           // 307:Temporary Redirect
           // 400:Bad Request
           // 401:Unauthorized
           // 402:Payment Required
           // 403:Forbidden
           // 404:Not Found
           // 405:Method Not Allowed
           // 406:Not Acceptable
           // 407:Proxy Authentication Required
           // 408:Request Timeout
           // 409:Conflict
           // 410:Gone
           // 411:Length Required
           // 412:Precondition Failed
           // 413:Request Entity Too Large
           // 414:Request-URI Too Long
           // 415:Unsupported Media Type
           // 416:Requested Range Not Suitable
           // 417:Expectation Failed
           // 500:Internal Server Error
           // 501:Not Implemented
           // 502:Bad Gateway
           // 503:Service Unavailable
           // 504:Gateway Timeout
           // 505:HTTP Version Not Supported
           //   备注:长整形,此属性只读,返回当前请求的 http 状态码 , 此属性仅当数据发送并接收完毕后才可获取。
       alert(xmlhttp.status);

8、   属性: statusText
         //   返回当前请求的响应行状态
         //   语法: strValue = oXMLHttpRequest.statusText;
         //   备注:字符串,此属性只读,以 BSTR 返回当前请求的响应行状态 , 此属性仅当数据发送并接收完毕后才可获取。
       alert(xmlhttp.statusText);

9 属性: readyState
      //   返回 XMLHTTP 请求的当前状态
      //   语法: lValue = oXMLHttpRequest.readyState;
      //   备注:变量,此属性只读,状态用长度为的整型表示定义如下:
      // 0 ( 未初始化 )   对象已建立,但是尚未初始化(尚未调用 open 方法)
      // 1 ( 初始化 )   对象已建立,尚未调用 send 方法
      // 2 ( 发送数据 ) send 方法已调用,但是当前的状态及 http 头未知
      // 3 ( 数据传送中 )   已接收部分数据,因为响应及 http 头不全,这时通过 responseBody responseText 获取部分数据会出现错误,
      // 4 ( 完成 )   数据接收完毕 , 此时可以通过通过 responseBody responseText 获取完整的回应数据
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值