JeanValjean
JeanValjean

Reputation: 17713

XmlHttpRequest in Symfony2

Here follows my problem. In a Symfony2.1 controller I receive an Ajax call. If I use the Request object I'm not able to get the sent param. If I use the PHP _REQUEST object the param is found! I can confirm that the request is XHR, if it matters. Here is my code:

public function savedataAction(Request $request){  
  if($request->isXmlHttpRequest())
  echo 'Ajax Call';

  $param1 = $request->request->get('myParam'); // Nothing is returned, but $request is obviosly not null
  $param2 = $_REQUEST['myParam']; // The value is given

  ....
}

Any idea?

PS: If helps, notice that the AJAX call is sent by the file uploader jQuery plugin provided by Valums.

Upvotes: 2

Views: 7201

Answers (1)

René Höhle
René Höhle

Reputation: 27295

Normally its:

// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');

Look here are the fundamentels.

http://symfony.com/doc/current/book/http_fundamentals.html

Edit:

$request in your case is only filled when you send the form from the Symfony2 site. It is possible, that the CSRF protection block the request.

Try this in your controller to get the Request object:

$request = $this->get('request');

http://symfony2forum.org/threads/5-Using-Symfony2-jQuery-and-Ajax

Upvotes: 1

Related Questions