i am learning begging step of creating a ajax request in magento and mostly i found same code logic. But where i am confused in these tutorials are actually what controller will return?


in ajax request controller action is returning.


which is same as non ajax call. Actually what this code return when call via ajax? complete html, head, body & their inner html tags? what if i want to retun some specification or json data?

i am asking 2 things

  • what given code controller->action code return when call via ajax?
  • how to return specific

1 Answer 1


you can return pure json directly in the controller: http://www.kathirvel.com/magento-returning-json-for-ajax-and-api-calls-response-from-controller-action/

if you want to render the json from a block (over layout) you can override the root block so that only your block is rendered and then add your json-rendering block as root like here: https://github.com/bragento/magento-core/blob/1.9/app/design/frontend/base/default/layout/checkout.xml#L364

(This is quite a bit different since the CheckoutController creates a json response and renders the html generated by the block into a node of the json object)

  • can you tell me what this code return when cotroller call via ajax? $this->loadLayout(); $this->renderLayout(); Feb 6, 2016 at 19:28
  • it will return whatever is defined in your layout. This means first it adds any block defined in the default layout handle (under app/design/frontend/base/default/layout in most layout files you will find the default node). You can add blocks to your layout handle by defining a layout with your actions layout handle: <frontname>_<controllername>_<actionname>. Here you'll find an example: blog.baobaz.com/en/blog/… Feb 6, 2016 at 19:32
  • I know what it's return when call it simply then why people call it in ajax request? When it's return complete page html? Because page i s already loaded Feb 6, 2016 at 19:35
  • there is no difference what the controller returns when you call it via an ajax request (except you enforce it with a parameter - for example is_ajax=true and then change the controllers behaviour). To avoid having a complete page html you have to replace the root block in your layout with your own, since all other blocks (like header, footer, etc...) are children of the root block. Or you just directly return your response with the setBody method and load nothing from the layout Feb 6, 2016 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.