Skip to content

Latest commit

 

History

History
82 lines (63 loc) · 1.67 KB

README.md

File metadata and controls

82 lines (63 loc) · 1.67 KB

Oggetto_Ajax

Magento helper module for sending AJAX-responses

Install via composer

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.

Usage

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

Useful helper methods

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).