0

I use Symfony 3 and I try to call my function in the Default Controller with Ajax like this:

$(document).ready(function() {
    $.ajax({
        dataType: "json",
        type: "POST",
        data: {
            action: 'active'
        },
        url: '{{ path ('
        show_chat ')}}',
        success: function(active) {
            if (active === true)
                $('.message-box').fadeIn();
        },
        error: function() {
            console.log('error');
            return false;
        }
    });
});

In the Default Controller I want to return a twig template and a variable which is true so my window for the chat can appear:

return $this->render('ChatBundle:Default:chat.html.twig',$online);

I use $presence = $request->get('active'); Symfony\Component\HttpFoundation\Response;

My method is POST but when I dump $request the method is GET. I do not understand what I am doing wrong and why it doesn't work ... Any ideas, please

Routing:

show_chat:
path:     /chat
defaults: { _controller: slackChatBundle:Default:chat}
methods: [POST,GET]

If I put only POST method ,I have a message "No route found,Method Not Allowed (Allow: POST)

this is the dump of request:

dump request

dw2017
  • 1
  • 3
  • the request method is Post .. – dw2017 Nov 14 '17 at 07:20
  • You are getting it correctly. Just look into https://stackoverflow.com/a/9788435/2236219. Both GET & POST methods having the different way to be accessed in the controller. – Manwal Nov 14 '17 at 07:25
  • I saw this answer, I have tried but still it doesn't work – dw2017 Nov 14 '17 at 07:29
  • I have used both $request->query->get('name'); and $request->request->get('name') but none of them seems to do the job. That is why I have no idea what is wrong with my code – dw2017 Nov 14 '17 at 07:32
  • Cou give us a dump of `$request` in question? – Manwal Nov 14 '17 at 08:52
  • Try with `method` instead of `type` in your Jq ajax call. Check in your browser `dev tool > network > xhr` if the request have the good HTTP verb – Mcsky Nov 14 '17 at 09:31
  • Mcsky, thank you for the proposition ,but it doesn't work...all seems fine in network , I can see the action the method . – dw2017 Nov 14 '17 at 10:03

2 Answers2

0

Can you provide more specific dumps request->request & request->query ?

And related to following post, I agree you should use FOSJsRoutingBundle. It let you expose symfony routing.

karael
  • 19
  • 1
  • 6
  • I add a photo of dump $request. It is with Get,everything is empty – dw2017 Nov 14 '17 at 09:15
  • Ok I can comment now, I need theses specific dumps to see if problem come from js (probably routing) or from your controller – karael Nov 14 '17 at 12:43
  • Karael, Thank you very much for your advices ...I managed to find a solution ..I did not changed a lot, I assign the variable to JsonResponse : $response = new JsonResponse($online) and I returned it return $response,no render and just work,suprise.... I do not know if it is good approach but it does work. ..Now I will use FOSJsRoutingBundle, because I have to make an ajax call from the js – dw2017 Nov 14 '17 at 15:30
0

I advice that you use FOSJsRoutingBundle that allows you to expose Symfony Routes to JavaScript

Mohamed Ben HEnda
  • 2,686
  • 1
  • 30
  • 44