Valentin BEAULE
Valentin BEAULE

Reputation: 404

Symfony3 send AJAX POST request

I want to send two variables id and commentary through an AJAX POST request. The problem is that I don't get the POST variable but the route is reached.

JS:

$.post(Routing.generate('ajax_savecommentary', { id:id, commentary:commentary }), 
function(response)
{
}, "json");

Symfony:

public function saveCommentaryAction()
{
    if (!$this->get('session')->get('compte'))
        return $this->redirect($this->generateUrl('accueil'));

    $request = $this->container->get('request_stack')->getCurrentRequest();
    $isAjax = $request->isXMLHttpRequest();

    if ($isAjax)
    {
        $information = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Information')->find($_POST['id']);

        $information->setCommentaire(str_replace('\n', '\\n', $_POST['commentary']));
        $this->getDoctrine()->getManager()->flush();

        $response = array("code" => 100, "success" => true, 'commentary' => $_POST['commentary']);
        return new Response(json_encode($response));
    }
    $response = array("code" => 0, "success" => false);
    return new Response(json_encode($response));
}

The error:

http://localhost/MyProject/web/app_dev.php/ajax/save/commentary/?id=61&commentary=MyCommentary.

{"code":0,"success":false}

More Symfony error:

GET Parameters

Key/Value

commentary/MyCommentary

id/61

And the routing is case needed:

ajax_savecommentary:
    defaults: { _controller: CommonBundle:Default:saveCommentary }
    path:     /ajax/save/commentary/
    options:
        expose: true

Upvotes: 3

Views: 1754

Answers (1)

Matteo
Matteo

Reputation: 39380

Try using the request passed to the Controller Action instead of retrieve it from the container. So try this:

use Symfony\Component\HttpFoundation\Request;

...

public function saveCommentaryAction(Request $request)
{
    if (!$this->get('session')->get('compte'))
        return $this->redirect($this->generateUrl('accueil'));

    $isAjax = $request->isXMLHttpRequest();

instead of this:

public function saveCommentaryAction()
{
    if (!$this->get('session')->get('compte'))
        return $this->redirect($this->generateUrl('accueil'));

    $request = $this->container->get('request_stack')->getCurrentRequest();
    $isAjax = $request->isXMLHttpRequest();

UPDATE:

You can restrict your routing with Customized Route Matching with Conditions, as example on your case as follow:

ajax_savecommentary:
    defaults: { _controller: CommonBundle:Default:saveCommentary }
    path:     /ajax/save/commentary/
    options:
        expose: true
    condition: "request.isXmlHttpRequest()"
    methods:  [POST]

UPDATE:

There is a typo in the routing generation in the JS side:

$.post(Routing.generate('ajax_savecommentary', { id:id, commentary:commentary }), 
function(response)
{
}, "json");

you pass the data as argument of the routing.generate function so it concatenate the params as query string. so try this:

$.post(Routing.generate('ajax_savecommentary'), { id:id, commentary:commentary }, 
function(response)
{
}, "json");

Another advice is about to use the $request object for obtain the data instead of the superglobal PHP attribute, so use:

$request->request-get('commentary');

instead of:

 $_POST['commentary']

More info here in the doc.

Hope this help

Upvotes: 2

Related Questions