Magento helper module for sending AJAX-responses
Update your composer.json
like this
"require": {
...
"oggettoweb/ajax": "1.*"
...
},
"repositories": [
...
{
"type": "vcs",
"url": "https://github.com/OggettoWeb/ajax"
}
],
...
"extra":{
"magento-root-dir": ".",
}
Optionally you can add
"extra":{
"magento-deploystrategy": "copy",
"auto-append-gitignore": true
}
See more information about composer installer for magento at github repository.
In the controller action which should respond to AJAX request write something like:
$response = Mage::getModel('ajax/response');
try {
// do things
$response->success()->setFoo('bar');
} catch(Exception $e) {
$response->error()->setMessage($e->getMessage());
}
Mage::helper('ajax')->sendResponse($response);
If everything is OK you will have the following JSON in the HTTP response body:
{"status":"success","foo":"bar"}
And in case of exception:
{"status":"error","message":"exception message"}
In both cases HTTP response will also have Content-Type
header set:
Content-Type: application/json
There are two useful methods which you may like to use:
Mage::helper('ajax')->sendError($message);
Mage::helper('ajax')->sendSuccess($data);
These methods will automatically build ajax-response object and send it to HTTP response (just like described above).