Skip to content

Controller

isAjax()

isAjax() — returns boolean

Available in: controller Category: Miscellaneous Functions

Returns whether the page was called from JavaScript or not.

// 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();
}