Ajax Class
Create a new AJAX requester.
Constructor
Ajax
(
-
url -
options
Parameters:
-
urlStringURL to load
-
optionsObjectOptions, which may consist of the following:
- method:
POSTorGET - onSuccess, onFailure, onComplete: Callbacks for when the specified event happens
- format:
jsonortext, format to interpret result as - data: GET or POST data to send when doing request
- context: What "this" should be when callback is called
- method:
Example:
// Basic GET request:
var request = new Ajax('test.php',
{
onSuccess: function(data)
{
console.log(data);
}
});
request.send();
// POST request
// Uses Util.buildQueryString to encode POST data. Arrays and hashes are sent using PHP syntax
(new Ajax('test.php',
{
data:
{
'hello': 'world',
// Supports arrays
'arrayTest': [1, 2, 3, 4, 5],
// Supports hashes
'hashText':
{
'awesome': true,
'something': 'else'
}
},
onSuccess: function(data)
{
console.log(data);
}
})).send();
// Alternatively, the data can be passed to the send() method directly:
(new Ajax('test.php',
{
onSuccess: function(data)
{
console.log(data);
}
})).send({hello: 'world'});
Methods
_getXHR
()
private
Internal function, get an XMLHttpRequest object
Returns:
XMLHttpRequest object
_onComplete
(
private
-
xhr -
requestId
Internal function, called when request completes
Parameters:
-
xhrXMLHttpRequestXHR object used to send request
-
requestIdIntegerRequest ID
send
(
-
data
Send this AJAX request
Parameters:
-
dataObjectQuery string data to send, optional.
Properties
currentRequest
XMLHttpRequest
private
Current request, if any
options
Object
Options for the AJAX request
requestCount
Integer
private
static
Count of how many requests have been done
requests
Object
private
static
All the currently executing requests
url
String
URL the request will be to
