Controller
isAjax()
Signature
Section titled “Signature”isAjax() — returns boolean
Available in: controller
Category: Miscellaneous Functions
Description
Section titled “Description”Returns whether the page was called from JavaScript or not.
Examples
Section titled “Examples”// 1. Respond differently based on whether the request is an AJAX call
if (isAjax()) {
renderNothing();
} else {
redirectTo(action = "index");
}
// 2. Return JSON for AJAX requests, render a full view otherwise
if (isAjax()) {
renderWith(data = model("Article").findAll(returnAs = "objects"));
} else {
articles = model("Article").findAll();
}