📜  DOM-XMLHttpRequest对象

📅  最后修改于: 2020-11-10 05:15:07             🧑  作者: Mango


XMLHttpRequest对象在网页的客户端和服务器端之间建立了一种媒介,JavaScript,JScript,VBScript和其他网络浏览器等许多脚本语言都可以使用该媒介来传输和处理XML数据。

使用XMLHttpRequest对象,可以在不重新加载整个页面的情况下更新网页的一部分,在页面加载后将请求从服务器接收并从服务器接收数据并将数据发送到服务器。

句法

一个XMLHttpRequest对象可以被实例化为-

xmlhttp = new XMLHttpRequest();

要处理所有浏览器,包括IE5和IE6,请检查浏览器是否支持XMLHttpRequest对象,如下所示:

if(window.XMLHttpRequest) // for Firefox, IE7+, Opera, Safari, ... {
   xmlHttp = new XMLHttpRequest();
} else if(window.ActiveXObject) // for Internet Explorer 5 or 6 {
   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

实施例使用XMLHttpRequest对象加载XML文件可被称作这里

方法

下表列出了XMLHttpRequest对象的方法-

S.No. Method & Description
1

abort()

Terminates the current request made.

2

getAllResponseHeaders()

Returns all the response headers as a string, or null if no response has been received.

3

getResponseHeader()

Returns the string containing the text of the specified header, or null if either the response has not yet been received or the header doesn’t exist in the response.

4

open(method,url,async,uname,pswd)

It is used in conjugation with the Send method to send the request to the server. The open method specifies the following parameters −

  • method − specifies the type of request i.e. Get or Post.

  • url − it is the location of the file.

  • async − indicates how the request should be handled. It is boolean value. where,

    • ‘true’ means the request is processed asynchronously without waiting for a Http response.

    • ‘false’ means the request is processed synchronously after receiving the Http response.

  • uname − is the username.

  • pswd − is the password.

5

send(string)

It is used to send the request working in conjugation with the Open method.

6

setRequestHeader()

Header contains the label/value pair to which the request is sent.

属性

下表列出了XMLHttpRequest对象的属性-

S.No. Attribute & Description
1

onreadystatechange

It is an event based property which is set on at every state change.

2

readyState

This describes the present state of the XMLHttpRequest object. There are five possible states of the readyState property −

  • readyState = 0 − means request is yet to initialize.

  • readyState = 1 − request is set.

  • readyState = 2 − request is sent.

  • readyState = 3 − request is processing.

  • readyState = 4 − request is completed.

3

responseText

This property is used when the response from the server is a text file.

4

responseXML

This property is used when the response from the server is an XML file.

5

status

Gives the status of the Http request object as a number. For example, “404” or “200”.

6

statusText

Gives the status of the Http request object as a string. For example, “Not Found” or “OK”.

例子

node.xml的内容如下-



   
      Tanmay
      Patil
      1234567890
      tanmaypatil@xyz.com
   
   
   
      Taniya
      Mishra
      1234667898
      taniyamishra@xyz.com
   
   
   
      Tanisha
      Sharma
      1234562350
      tanishasharma@xyz.com
   

检索资源文件的特定信息

下面的示例演示如何使用方法getResponseHeader()和属性readState检索资源文件的特定信息。


         
   
   
      
      
Specific header information is returned.

执行

将此文件另存为服务器路径上的elementattribute_removeAttributeNS.htm (此文件和node_ns.xml应位于服务器中的同一路径上)。我们将获得如下所示的输出-

Before removing the attributeNS: en
After removing the attributeNS: null

检索资源文件的标头信息

下面的示例演示如何通过使用属性readyState的getAllResponseHeaders()方法检索资源文件的头信息。


      
   
   
      
      

执行

在服务器路径上将此文件另存为http_allheader.html (此文件和node.xml应在服务器中的同一路径上)。我们将获得如下所示的输出(取决于浏览器)-

Date: Sat, 27 Sep 2014 07:48:07 GMT Server: Apache Last-Modified: 
      Wed, 03 Sep 2014 06:35:30 GMT Etag: "464bf9-2af-50223713b8a60" Accept-Ranges: bytes Vary: Accept-Encoding,User-Agent 
      Content-Encoding: gzip Content-Length: 256 Content-Type: text/xml