Ajax requests are fundamentally the same as normal web requests in ColdMVC: the request is matched to a route and handled by a controller, which gathers data from the model and optionally renders a view.
However, there are a few things to know when working with Ajax.
There is not a layout associated with Ajax requests by default. This is because most of the time your Ajax requests will return a small snippet of HTML, or possiblly a JSON or XML string. If you would like to wrap your Ajax views inside a layout during Ajax requests, you would add an @ajaxLayout annotation to your controller's actions.
/**
* @ajaxLayout modal
* /
function search() {
params.results = _Book.findAllByTitleLike(params.q);
}
You will also need to allow your action to respond to the format by adding a @formats annotation to the action on your controller. By default, all actions only respond to a html and pdf formats.
/**
* @formats json
* /
function results() {
params.results = _Book.findAllByTitleLike(params.q);
}
If you would like your Ajax request to return something other than HTML, like a JSON or XML string, you can have the response automatically serialized into the proper format by adding a format parameter to your Ajax request.
$('#results').load('#linkTo({controller="index", action="search"})#', {
format: 'json',
q: $('#q').val()
});
There is no debug information appended to your request during an Ajax request. This is to prevent the Ajax request from returning too much data or breaking the response format.