27.10.2015 Views

AJAX and PHP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You will learn the most interesting details about XMLHttpRequest by practice, but for a quick<br />

reference here are the object's methods <strong>and</strong> properties:<br />

Chapter 2<br />

Method/Property<br />

abort()<br />

getAllResponseHeaders()<br />

getResponseHeader("headerLabel")<br />

open("method", "URL"[, asyncFlag[,<br />

"userName"[, "password"]]])<br />

send(content)<br />

setRequestHeader("label", "value")<br />

onreadystatechange<br />

readyState<br />

responseText<br />

responseXML<br />

Status<br />

statusText<br />

Description<br />

Stops the current request.<br />

Returns the response headers as a string.<br />

Returns a single response header as a string.<br />

Initializes the request parameters.<br />

Performs the HTTP request.<br />

Sets a label/value pair to the request header.<br />

Used to set the callback function that h<strong>and</strong>les request<br />

state changes.<br />

Returns the status of the request:<br />

0 = uninitialized<br />

1 = loading<br />

2 = loaded<br />

3 = interactive<br />

4 = complete<br />

Returns the server response as a string.<br />

Returns the server response as an XML document.<br />

Returns the status code of the request.<br />

Returns the status message of the request.<br />

The methods you will use with every server request are open <strong>and</strong> send. The open method<br />

configures a request by setting various parameters, <strong>and</strong> send makes the request (accesses the<br />

server). When the request is made asynchronously, before calling send you will also need to set<br />

the onreadystatechange property with the callback method to be executed when the status of the<br />

request changes, thus enabling the <strong>AJAX</strong> mechanism.<br />

The open method is used for initializing a request. It has two required parameters <strong>and</strong> a few<br />

optional ones. The open method doesn't initiate a connection to the server; it is only used to set the<br />

connection options. The first parameter specifies the method used to send data to the server page,<br />

<strong>and</strong> it can have a value of GET, POST, or PUT. The second parameter is URL, which specifies where<br />

you want to send the request. The URL can be complete or relative. If the URL doesn't specify a<br />

resource accessible via HTTP, the first parameter is ignored.<br />

47<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!