AJAX in Components in Joomla
During a conversation at JUG London I was asked about the easy of building AJAX into a component and was asked whether the right way to do it was via com_ajax.
Why is com_ajax the wrong way?
The ajax component introduced in Joomla 3.2 as per the documentation is designed to provide AJAX support to standalone plugins and modules. Components can actually support ajax requests without needing to use com_ajax.
What is the right way?
You can build your MVC the "right" way to support AJAX and in fact it's as simple as calling a URL with format=json in the url with a task with the format CONTROLLER_NAME.TASK_NAME. The controller file name also has a special constraint in that it must contain the format type between the controller name and .php. So for example the url /administrator?option=com_foobar&task=foo.bar&format=json will look in JPATH_ADMINISTRATOR/components/com_foobar/controllers/foo.json.php and try and execute the method bar.
I have developed a simple component for people to browse which can be <a href="http://georges.website/downloads/com_dropdowns.zip">downloaded here</a>. Install it onto a site like a regular Joomla component and browse the code at your leisure
More complex examples in Joomla
The only other example in Joomla Core of com_ajax is the index of the Smart Search component