Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Magento helper module for sending AJAX-responses
PHP
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
app
README.md
composer.json

README.md

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

Something went wrong with that request. Please try again.