XML DOM XMLHttpRequest Object - XML DOM

What is XMLHttpRequest object of XML DOM?

A medium or a bridge is established between the client-side and the server-side of a web page by XMLHttpRequest object. In order to transfer and manipulate the XML data, this medium uses many languages like JavaScript, JScript, VBScript and other web browser.

XMLHttpRequest object facilitates in updating a part of the web page without reloading the complete page. This is done by requesting and receiving the data from the server and once the page is loaded, the data is sent to the server.

Syntax

The syntax for the XMLHttpRequest object is as follows −

The browser is checked whether it supports the XMLHttpRequest object as follows:

Some of the illustrations for loading XML files with the help of XMLHttpRequest object are discussed in the chapter XML DOM Loading.

What are the different methods of XMLHttpRequest object provided by XML DOM?

Methods of XMLHttpRequest object supported by XML DOM are as follows:

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,
o 'true' means the request is processed asynchronously without waiting for a Http response.
o '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.

What are the attributes of XMLHttpRequest Object?

The attributes of the XMLHttpRequest object are as follows:

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".

Examples

The contents of node.xml are as follows:

Retrieve specific information of a resource file

An illustration is considered for demonstrating the process of retrieving the information from a resource file with the help of getResponseHeader() method and the readState property.

Execution

The file is saved as elementattribute_removeAttributeNS.htm on the server path and this file should be on the same path as that of node.xml in the server. The output appears as follows:

Retrieve header infomation of a resource file

An illustration is considered for demonstrating the process of retrieving the header information from a resource file with the help of getAllResponseHeaders() method and the readyState property.

Execution

The file is saved as http_allheader.html on the server path and this file should be on the same path as that of node.xml in the server. The output appears as follows:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML DOM Topics