Adding Ajax Functionality

Module authors wishing to add ajax functionality should target the following URL in their JavaScripts:

"<?php echo url('/', 'AjaxController'); ?>"

The ajax controller offers a trigger-based framework for responding to ajax requests.

Responding to Ajax Requests

You must provide a unique route action in your ajax GET or POST request in order for your request to be routed to a responder in your module. Adding an ajax_foobarbaz() trigger function to your module will enable your module to respond to requests sent with a POST action of foobarbaz or sent to /ajax/?action=foobarbaz.

Your trigger function should return an appropriate response object to your JavaScript on either success or failure. If your function returns false the routing process will continue, and if no other responders are found, it will conclude with a 404 error.

JSON Responses

Module authors are encouraged to use the helper function json_response() when responding to ajax requests. json_response() encodes a response object containing two properties: text and data. Authors are encouraged to always populate the text property with a human-readable response message. The data property can contain any arbitrary JSON-encodable data.