Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
The XML Http Request Object

The XML Http Request Object

Ratings:
(0)
|Views: 159|Likes:
Published by api-3750475

More info:

Published by: api-3750475 on Oct 19, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Search
Advanced Search
Log In| Not a Member?
Contact ADC
ADC Home> Internet & Web> Web Content>

As deployment of XML data and web services becomes more widespread, you may occasionally find it
convenient to connect an HTML presentation directly to XML data for interim updates without reloading the
page. Thanks to the little\u00adknownXMLHttpRequest object, an increasing range of web clients can retrieve and
submit XML data directly, all in the background. To convert retrieved XML data into renderable HTML content,
rely on the client\u00adside Document Object Model (DOM) to read the XML document node tree and compose HTML
elements that the user sees.

History and Support

Microsoft first implemented theXMLHttpRequest object in Internet Explorer 5 for Windows as an ActiveX
object. Engineers on the Mozilla project implemented a compatible native version for Mozilla 1.0 (and Netscape
7). Apple has done the same starting with Safari 1.2.

Similar functionality is covered in a proposed W3C standard,Document Object Model (DOM) Level 3 Load and
Save Specification. In the meantime, growing support for theXMLHttpRequest object means that is has
become ade facto standard that will likely be supported even after the W3C specification becomes final and
starts being implemented in released browsers (whenever that might be).
Creating the Object

Creating an instance of theXMLHttpRequest object requires branching syntax to account for browser
differences in the way instances of the object are generated. For Safari and Mozilla, a simple call to the object's
constructor function does the job:

var req = new XMLHttpRequest()\u037e
For the ActiveX branch, pass the name of the object to the ActiveX constructor:
var req = new ActiveXObject("Microsoft.XMLHTTP")\u037e

The object reference returned by both constructors is to an abstract object that works entirely out of view of the user. Its methods control all operations, while its properties hold, among other things, various data pieces returned from the server.

Object Methods

Instances of theXMLHttpRequest object in all supported environments share a concise, but powerful, list of methods and properties. Table 1 shows the methods supported by Safari 1.2, Mozilla, and Windows IE 5 or later.

Table 1. Common XMLHttpRequest Object Methods

Of the methods shown in Table 1, theopen() andsend() methods are the ones you'll likely use most. The
first,open(), sets the scene for an upcoming operation. Two required parameters are the HTTP method you
intend for the request and the URL for the connection. For the method parameter, use"GET" on operations that
are primarily data retrieval requests\u037e use"POST" on operations that send data to the server, especially if the
length of the outgoing data is potentially greater than 512 bytes. The URL may be either a complete or relative
URL (but see security issues below).

An important optional third parameter is a Boolean value that controls whether the upcoming transaction
Method
Description
abort()
Stops the current request
getAllResponseHeaders()
Returns complete set of headers (labels and values)
as a string
getResponseHeader("headerLabel")
Returns the string value of a single header label
open("method", "URL"[,asyncFlag[,
"userName"[, "password"]]])
Assigns destination URL, method, and other optional
attributes of a pending request
send(content)
Transmits the request, optionally with postable
string or DOM object data
setRequestHeader("label", "value")
Assigns a label/value pair to the header to be sent
with a request

should be handled asynchronously. The default behavior (true) is to act asynchronously, which means that
script processing carries on immediately after thesend() method is invoked, without waiting for a response. If
you set this value tofalse, however, the script waits for the request to be sent and for a response to arrive
from the server. While it might seem like a good idea to wait for a response before continuing processing, you
run the risk of having your script hang if a network or server problem prevents completion of the transaction. It
is safer to send asynchronously and design your code around theonreadystatechange event for the request

object.

The following generic function includes branched object creation, event handler assignment, and submission of
a GET request. A single function argument is a string containing the desired URL. The function assumes that a
global variable,req, receives the value returned from the object constructors. Using a global variable here
allows the response values to be accessed freely inside other functions elsewhere on the page. Also assumed in
this example is the existence of aprocessReqChange() function that will handle changes to the state of the
request object.

var req\u037e
function loadXMLDoc(url) {

req = false\u037e
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest) {

try {
req = new XMLHttpRequest()\u037e
} catch(e) {
req = false\u037e
}
// branch for IE/Windows ActiveX version

} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP")\u037e
} catch(e) {
try {

req = new ActiveXObject("Microsoft.XMLHTTP")\u037e
} catch(e) {
req = false\u037e
}
}
}
if(req) {req.onreadystatechange = processReqChange\u037e
req.open("GET", url, true)\u037e
req.send("")\u037e
}
}

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->