1

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?

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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.