Methods
Ajax.get()
This is used to send information to the server with a GET request.
example: Request the test.php page without returning a response.
Ajax.get({ 'url' : 'test.php' });
Ajax.post()
This is used to send information to the server with a POST request.
example: Request the test.php page without returning a response.
Ajax.post({ 'url' : 'test.php' });
Ajax.pause()
This is used to send information to the server with a GET or POST request.
This method is used when you don't want the request to begin right away. This is useful for onkeyup calls to an AJAX function. The default method is GET, but it can be changed to POST with the method property. The default pause time is 500 milliseconds.
example: Request the test.php page without returning a response.
Ajax.pause({ 'url' : 'test.php' });
Properties
url
A string containing the URL to which the request is sent. This is the only property that is required for all requests.
example: Request the test.php page without returning a response.
Ajax.get({ 'url' : 'test.php' });
method (optional)
A string specifying the type of request to be sent. The value should be either "GET" or "POST". This property is usually set by the type of method you run. The only time it must be specified is when using the AJAX.pause method and you require a POST type request.
example: Request the test.php page, using the POST type request, and without returning a response.
Ajax.pause({ 'url' : 'test.php', 'method' : 'POST' });
parameters (optional)
An object containing the variables which will be sent along with the request.
example: Request the test.php page, sending two variables, and without returning a response.
Ajax.get({
'url' : 'test.php',
'parameters' : { 'var1' : 'foo', 'var2' : 'bar' }
});
element (optional)
A string specifying the id of an HTML element to output a response.
example: Request the test.php page and insert the response into an HTML element with the unique id of "results".
Ajax.get({
'url' : 'test.php',
'element' : 'results'
});
outputtype (optional)
A string that defines how to output to the specified element. The value should be either "innerHTML" or "input" The default option is "innerHTML". The only time this property is required is when the response is to be inserted into a form element such as an input.
example: Request the test.php page and insert the response into a form element with the unique id of "results".
Ajax.get({
'url' : 'test.php',
'element' : 'results',
'outputtype' : 'input'
});
pause (optional)
An integer that defines the number of milliseconds to wait before executing the request. The default value is 500.
example: Request the test.php page after waiting 3 seconds and without returning a response.
Ajax.pause({
'url' : 'test.php',
'pause' : 3000
});
onSuccess (optional)
A function to be executed after a successful response. The response can be sent to a function by defining a parameter, and accessed through a property called responseText
example: Request the test.php, and on success execute a function with a parameter of "req". This function will then display the response in an alert dialog box.
Ajax.get({
'url' : 'test.php',
'onSuccess' : function(req) { alert(req.responseText); }
});
onError (optional)
A function to be executed if an error occurs. This property is similar to the onSuccess property.
example: Request the test.php, and on error execute a function with a parameter of "req". This function will then display the error in an alert dialog box.
Ajax.get({
'url' : 'test.php',
'onError' : function(req) { alert(req.errorText); }
});
spinner (optional)
A string that specifies a path to an image and a message separated by a comma. This property requires that the "element" property is also set. It will insert an image into the specified element until a response is returned.
example: Request the test.php, and insert a spinner.gif and the message "Loading..." into the result element.
Ajax.get({
'url' : 'test.php',
'element' : 'result',
'spinner' : '/images/spinner.gif, Loading...'
});
responseText
A string that contains the request response. Once a request is sent successfully, the response is stored within the Ajax object. Although, the response can then be accessed thought this property, it usually isn't practical, since you may not know when the successful response will be available.
The preferred method to access this property is shown in the "onSuccess" example above.
example: Request the test.php, and set the response to a variable called "retn".
Ajax.get({ 'url' : 'test.php' });
var retn = Ajax.responseText;
errorText
A string that contains the request error message. If an error occurs during a request, the response is stored within the Ajax object.
Similar to the responseText property, the preferred method to access this property is shown in the "onError" example above.
example: Request the test.php, and set the error to a variable called "err".
Ajax.get({ 'url' : 'test.php' });
var err = Ajax.errorText;
View Demo Download