XHR.js

Contains the basic XMLHttpRequest Class Wrapper.

License

MIT-style license.

Summary
XHR.jsContains the basic XMLHttpRequest Class Wrapper.
XHRBasic XMLHttpRequest Wrapper.
Properties
setHeaderAdd/modify an header for the request.
sendOpens the XHR connection and sends the data.
cancelCancels the running request.

XHR

Basic XMLHttpRequest Wrapper.

Arguments

optionsan object with options names as keys.  See options below.

Options

method’post’ or ‘get’ - the protocol for the request; optional, defaults to ‘post’.
asyncboolean: asynchronous option; true uses asynchronous requests.  Defaults to true.
encodingthe encoding, defaults to utf-8.
autoCancelcancels the already running request if another one is sent. defaults to false.
headersaccepts an object, that will be set to request headers.

Events

onRequestfunction to execute when the XHR request is fired.
onSuccessfunction to execute when the XHR request completes.
onStateChangefunction to execute when the state of the XMLHttpRequest changes.
onFailurefunction to execute when the state of the XMLHttpRequest changes.

Properties

runningtrue if the request is running.
responseobject, text and xml as keys.  You can access this property in the onSuccess event.

Example

var myXHR = new XHR({method: 'get'}).send('http://site.com/requestHandler.php', 'id=john&lastid=dorian');
Summary
Properties
setHeaderAdd/modify an header for the request.
sendOpens the XHR connection and sends the data.
cancelCancels the running request.

Properties

setHeader

Add/modify an header for the request.  It will not override headers from the options.

Example

var myXhr = new XHR(url, {method: 'get', headers: {'X-Request': 'JSON'}});
myXhr.setHeader('Last-Modified','Sat, 1 Jan 2005 05:00:00 GMT');

send

Opens the XHR connection and sends the data.  Data has to be null or a string.

Example

var myXhr = new XHR({method: 'post'});
myXhr.send(url, querystring);
 
var syncXhr = new XHR({async: false, method: 'post'});
syncXhr.send(url, null);

cancel

Cancels the running request.  No effect if the request is not running.

Example

var myXhr = new XHR({method: 'get'}).send(url);
myXhr.cancel();

Documentation by Aaron Newton & Mootools Developers, generated by NaturalDocs and GeSHi