3

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Well it worked about the isAjax condition. But I still can't access the $_POST variables. Actually I found out they're sent as Get variable and not post, and if I look at $_GET['id'] it works. For me it still not fixed.
Hi @ValentinBEAULE sorry i didn't see how do you access to the request data. In symfony is preferred to use this approach so don't use the superglobal variables.
@ValentinBEAULE you generate a wrong route and put the data in the route instead o passing as third arguments to the jquery post function
Thanks for your methode, I'll use it for now. About the route, how is it wrong? I do access to the function. Have you an example? Don't hesitate to post an answer as you almost already resolved the problem.

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.