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