so I'm trying to post json data to a symfony controller. I've
$(document).ready(function(){
$("#submit-button").click(function(){
$.ajax({
type: "POST",
url: "/registerTransaction",
data: { "data" : 'test' },
success: function(response) {
console.log(response);
},
contentType: "application/json",
dataType: 'json'
});
});
});
and for the controller
<?php
namespace Test\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class TransactionController extends Controller
{
public function registerTransactionAction()
{
$request = $this->container->get('request');
$data = $request->get('data');
var_dump($data);
die;
}
}
My routing.yml is
test_my_register:
path: /registerTransaction
defaults: { _controller: TestMyBundle:Transaction:registerTransaction }
But all I get is null for response. So what am I doing wrong?
Had to change
data: { "data" : 'test' },
to
data: '{ "data" : 'test' }',
and use in the controller
$data = $this->get("request")->getContent();
if(!empty($data)) {
$params = json_decode($data, true);
}