0

trying to send $http.post request from AngularJS form to Symfony controller to add form content to database. I can get success response with "status": 200 at AngularJS side.

However, at Symfony controller, $request->getContent() is empty, returns nothing; $request->getMethod() returns 'GET', which doesn't make sence to me. How can I get post data in Symfony??

P.S. Installed FOSRestBundle and enabled body listener, param fetcher listener.

I know my question is duplicated with POST Ajax request by AngularJS to Symfony controller, but 2 answers of this post didn't work for me.

My blog.html.twig,

<form name="replyForm" ng-submit="sendReply(blog.id)">
Preview: {{'{{reply.content}}'}} <br><br>
My Reply: <input type="text" name="content" ng-model="reply.content" />
<input type="submit" value="Reply" />                  
</form>

blog.js,

$scope.reply = {};  
$scope.sendReply = function(blogId){
    $scope.reply.blog = blogId;
    $http.post('http://localhost/app_dev.php/blogReply', $scope.reply, {'Content-Type': 'application/x-www-form-urlencoded'})
    .then(function(data){
        $scope.message = angular.fromJson(data);            
    }, function(data){
        $scope.message = angular.fromJson(data) || "Request failed";
    });
    $scope.reply = {};
}

Symfony blogController,

/**
* @Route("/blogReply", name="blogReply")
*/
public function replyAction(Request $request){
    $data = $request->request->get('data', 'default value if data does not exist');
    return new Response ($data); 
}

The result is 'default value if data does not exist'.

2
  • Hi, I'm still stuck with this issue. I've tried all the possible ways that I found on google, no luck :( Additionally, my request in symfony is null, I mean, $request->request->all() returns an empty array. Any ideas please? Commented Jul 22, 2016 at 10:08
  • first check in google chrome devtoolbar network tab if the request has post data in it. Commented May 10, 2019 at 18:46

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.