Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
XMLHttpRequest
objectXMLHttpRequest
object that can fetch files from a web server
XMLHttpRequest
and Ajax for use in an online version of MS Outlook (credit where it's due!)XMLHttpRequest
objectXMLHttpRequest
object requests page from serverXMLHttpRequest
fires an event when data arrives
XMLHttpRequest
XMLHttpRequest
methodsthe core JavaScript object that makes Ajax possible
Method | Description |
---|---|
open(method, url, async)
|
specifies the URL and HTTP request method |
send() send(postData)
|
sends the HTTP request to the server, with optional POST parameters |
abort()
|
stops the request |
getAllResponseHeaders() , getResponseHeader(name) , setRequestHeader(name, value)
|
for getting/setting raw HTTP headers |
XMLHttpRequest
propertiesProperty | Description |
---|---|
responseText
|
the entire text of the fetched page, as a string |
responseXML
|
the entire contents of the fetched page, as an XML document tree (seen later) |
status
|
the request's HTTP status code (200 = OK, etc.) |
statusText
|
HTTP status code text (e.g. "Bad Request" for 400) |
timeout
|
how many MS to wait before giving up and aborting the request (default 0 = wait forever) |
readyState
|
request's current state (0 = not initialized, 1 = set up, 2 = sent, 3 = in progress, 4 = complete) |
// this code is in some control's event handler
var ajax = new XMLHttpRequest();
ajax.open("GET", url, false);
ajax.send();
do something with ajax.responseText;
send
returns, the fetched text will be stored in request's responseText
propertyXMLHttpRequest
XMLHttpRequest
eventsEvent | Description |
---|---|
load
|
occurs when the request is completed |
error
|
occurs when the request fails |
timeout
|
occurs when the request times out |
abort
|
occurs when the request is aborted by calling abort()
|
loadstart , loadend , progress , readystatechange
|
progress events to track a request in progress |
var ajax = new XMLHttpRequest();
ajax.onload = functionName;
ajax.open("GET", url, true);
ajax.send();
...
function functionName() {
do something with this.responseText
;
}
load
eventthis
will refer to the ajax
objectresponseText
and other propertiesThere are two reasons a request might fail:
The server responded to our request, but it sent an error message instead of the data we requested.
(i.e., a server error occurred)
(i.e., a browser error occurred)
These two errors are handled differently.
var ajax = new XMLHttpRequest();
ajax.onload = functionName;
ajax.open("GET", "url", true);
ajax.send();
...
function functionName() {
if (this.status == 200) { // 200 means request succeeded
do something with this.responseText
;
} else { // any code other than 200 indicates an error message
code to handle the server error;
}
}
.onload
event just like a successful responsevar ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.onerror = browserErrorFunctionName; ajax.open("GET", "url", true); ajax.send(); ... function browserErrorFunctionName() { code to handle the browser error; }
.onerror
event of the XMLHttpRequest object
var ajax = new XMLHttpRequest();
...
ajax.onerror = ajaxBrowserError;
ajax.onload = handleResponse;
...
function handleResponse() {
if (this.status == 200) { // 200 means request succeeded
do something with this.responseText
;
} else { // any code other than 200 indicates an error message
alert('An Ajax error occurred!\n\n' +
'Error code: ' + this.status + '\n' +
'Error text: ' + this.statusText + '\n' +
'Full content of response:\n\n' + this.responseText);
}
}
function ajaxBrowserError() {
alert("An Ajax error occurred!\n\n" +
"The browser failed to contact the server. Either the browser " +
"security-blocked the request (to enforce the same-origin " +
"policy), or there is no internet connection.");
}
var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("GET", "url?name1=value1&name2=value2&...", true); ajax.send();
escape(string)
function on them
POST
requestvar params = new FormData(); params.append("name", value); params.append("name", value); var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("POST", "url", true); ajax.send(params);
FormData
object to gather your POST query parametersFormData
to the request's send
methodmethod
passed to open
should be changed to "POST"
XMLHttpRequest
security restrictions
http://www.foo.com/a/b/c.html
can only fetch from http://www.foo.com