The Ajax plugin helps you request data without refreshing the page. The plugin closely resembles the Prototype library's API. If you are looking for simple Ajax requests without Prototype's bluk, just include JavaScriptMVC's 'ajax' plugin.
Include the ajax plugin. This automatically gets included by many other plugins (controller, view, core).
include.plugins('ajax')
The ajax plugin provides one general purpose class, Ajax.Request, for ajax functionality. It is typically used to asynchronously request a response from a url, and to callback a function with the data from that response.
new Ajax.Request(url, options) -> XMLHttpRequest
This requests data from '/my_data.html' and shows the response's text in an alert box:
callback = function(transport){ // creates a callback function. Ajax.Request calls back with
alert(transport.responseText) // transport.responseText contains the text returned by the request
};
new Ajax.Request(
'/my_data.html', // the url we send the request to
{
method: 'get', // we perform a get request
onComplete: callback // sets the callback function
}
);
There are a few important points to notice about this example: