I have a class in my custom module that needs to handle various AJAX requests as well as a typical Magento request. I am using AngularJS on the front end.

Angular will submit request parameters in JSON format, so if you have a simple call such as:

$http.post('/mymodule/mycontroller/myaction', {param1: 'foo', param2: 'bar'});

You cannot simply use $this->getRequest()->getParams() to get param values. Instead you need to use file_get_contents(php://input) to get the raw input string which you can then json_decode or do whatever you need to to access the request params.

See: https://stackoverflow.com/questions/15707431/http-post-using-angular-js

As I said, my custom class needs to handle both AJAX requests from Angular as well as normal Magento requests (or even AJAX requests from jQuery). The problem is handling the parameters in a consistent fashion.

How can I normalize the request from Angular so that Magento still has access to $this->getRequest()->getParams()?

2 Answers 2


Use one action controller that has split functionality. Have your ajax request pass something like '&ajax=1' as a parameter. If this parameter exists then offer one functionality. If this parameter isn't set then offer the second.

public function controllerAction(){
    $jsonContents = file_get_contents(php://input);
       $jsonContents = $this->getRequest()->getParams();
    $contents = json_decode($jsonContents);
        $response = $this->ajaxRequest();
        $response = $this->normalRequest();

protected function ajaxRequest(){
    //return response;

protected function normalRequest(){
    //return redirect (or whatever);

Nice and simple.


I really wanted to keep things simple and I was not excited about using file_get_contents('php://input') all over the place. Here's what I used which should be all I need:

$ajaxRequest = json_decode(file_get_contents('php://input'), true);
foreach ($ajaxRequest as $key => $value) {
    $this->getRequest()->setPost($key, $value);
  • Oh, how does angular js compare -- in your opinion of course. Apr 13, 2015 at 18:50
  • 1
    Not really part of the scope of the question, but for the task I am working on (a very complex ordering form with inputs bound all over the place) it is far superior to what I came up with in jQuery.
    – pspahn
    Apr 13, 2015 at 20:52
  • Of course not within these limited bounds, but friendly banter heeds no boundary! Apr 13, 2015 at 20:58

Your Answer

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

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