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

7
  • the request method is Post .. Commented Nov 14, 2017 at 7:20
  • You are getting it correctly. Just look into stackoverflow.com/a/9788435/2236219. Both GET & POST methods having the different way to be accessed in the controller. Commented Nov 14, 2017 at 7:25
  • I saw this answer, I have tried but still it doesn't work Commented Nov 14, 2017 at 7: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 Commented Nov 14, 2017 at 7:32
  • Cou give us a dump of $request in question? Commented Nov 14, 2017 at 8:52

2 Answers 2

0

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

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

Comments

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.

3 Comments

I add a photo of dump $request. It is with Get,everything is empty
Ok I can comment now, I need theses specific dumps to see if problem come from js (probably routing) or from your controller
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

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.