0
votes

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

1
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? - Hippo
first check in google chrome devtoolbar network tab if the request has post data in it. - Lord Zed

1 Answers

0
votes
public function replyAction(Request $request) {
$data = json_decode($request->getContent(), true);
$request->request->replace($data);

echo $request->request->get('foosdaasd');
}