0

If someone plays a YouTube video I want to send this information to a Symfony Controller. But somehow the controller receives nothing.

The Ajax Post

 // Check if video is playing (works)
  var myPlayerState;
  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
        alert('playing');

         $.ajax({
              url: "{{ path('dbe_user_add_experience') }}",
              type: "POST",
              data: { "data" : 'test' },
              success: function(data) { 
                 alert (data);
              },
              error: function(XMLHttpRequest, textStatus, errorThrown)
              {
                alert('Error: ' +  errorThrown);
              }
           });
    }
    myPlayerState = event.data;
  }

The Routing

<route id="dbe_user_add_experience" pattern="/getexperience">
    <default key="_controller">FOSUserBundle:Level:getExperience</default>
</route>

The Controller

  public function getExperienceAction(){
        $request = $this->container->get('request');
        $data = $request->request->get('data');
        var_dump($data);
        die;


        $url = $this->container->get('router')->generate('fos_user_profile_show');
        $response = new RedirectResponse($url);
        return $response;

        //$this->addExperience(5);
  }
3
  • One thing is that you are using POST/->request and then checking the GET/->query part of the request. Commented Jul 3, 2014 at 10:18
  • okay, I fixed that. but I think somehow it's not sending the information at all. I checked with firebug > Networkanalysis and there is no POST method visible after the click. Commented Jul 3, 2014 at 10:35
  • Hey, is it sending the data in Json format? Commented Jul 3, 2014 at 13:25

3 Answers 3

1

Instead of

$data = $request->query->get('data');

Use

$data = $request->get('data');
Sign up to request clarification or add additional context in comments.

5 Comments

Okay, I fixed that part. But somehow I still don't see a POST method in the firebug networkanalysis.
do you get any javascript errors? is alert executed? is url properly parsed by twig?
Yes the alert is executed. How can I check if the url is properly parsed? I'm not sure if the ajax post is executed at all? I get one error message in the console: TypeError: $ is undefined
Try to use getexperience path hardcoded instead of path expression or check javascript code at your browser side, check javascript source if url is properly set
If you have $ is undefined it means that you don't have jQuery loaded, which means your ajax call can't be executed
1

You are using the wrong property to get POST data.

To get GET data :

$request->query->get('data');

To get POST data :

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

From http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Request.html

Comments

0

I found the error: Ajax was in the noconflict modus. Meaning I had to change $.ajax({ to jQuery.ajax({.

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.