1

I need to pass json data to my Symfony Controller. My ajax function looks like this:

var data = '{"firstname":"John"}';
$.ajax({  
type: "POST",  
url: save_url, //path to controller action  
data: {json:data},
success: function(response) {
     // Do something                
}
}); 

In my controller, I try to get my data through:

public function createAction(Request $request) {
    $data = $this->getRequest()->get('firstname');          
    return $this->render('MyBundle:Counter:test.html.twig', array(
        'data' => $data          
    )); 

Just to see if this works, I send $data to be echoed in a template. In Firebug I can see the data being sent and everything seems to work, but $data is empty and nothing is echoed. Where am I doing this wrong?

EDIT: When I check the response in Fireburg console, I see my data there, in place, but it never appears in the template. var_dump($data) tells that $data is null. So, it seems data is being sent but the controller ignores it.

4

4 回答 4

1

正如马雷克所注意到的:

$this->getRequest()

已经返回请求对象,您正在访问request请求的属性,这不会加起来。要么尝试:

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

或使用:

$data = $this->getRequest()->get('json');

您当然可以将 的返回值分配$this->getRequest()给一个变量,然后get从那里调用该 var 的方法......无论如何,这是我的初始答案,它确实包含更多提示,以及您可能会发现有用的注意事项:

应该能够以这种方式获取数据,尽管 AJAX 请求 + 在模板中回显?这听起来有点奇怪。我没有看到您将$data变量传递$this->render给任何地方的调用。

这是我的一个项目中控制器操作的复制粘贴位。它在那里工作得很好:

public function indexAction()
{
    if (!$this->getRequest()->isXmlHttpRequest())
    {//check if request is AJAX request, if not redirect
        return $this->redirect(
            $this->generateUrl('foo_bar_homepage')//changed this, of course
        );
    }
    $id = $this->getRequest()->get('id',false);//works fine

但是,我无法开始理解您为什么要这样做:

 var data = '{"firstname":"John"}';

为什么不简单地去:

$.ajax({
    type: "POST",
    url: url,//post how you get this URL please...
    data: {firstname: 'John'},//jQ will sort this out for you
    success: function(response)
    {
        console.log(response);
    }
    error: function()
    {
        console.log('an error occured');
        console.log(arguments);//get debugging!
    }
});

然后,在您的控制器中,您可以:

$this->getRequest()->get('firstname');//it should be John

您甚至可以{json:{firstname: 'john'}}将数据参数传递给$.ajax,您的控制器中唯一的区别是,您必须这样做:

$data = $this->getRequest()->get('json');
$firstName = $data['firstname'];

应该没问题除非有什么你没有告诉我们的:)

回顾:
这就是我要写的:

public function createAction()
{//no Request param in controller
    if (!$this->getRequest()->isXmlHttpRequest())
    {//no ajax request, no play...
        $this->redirect(
            $this->generateUrl('homepage_route')
        );
    }
    $data = $this->getRequest()->get('firstname');
    //return json response:
    return new Response(json_encode(array('dataReceived' => $data));
    //return rendered HTML page:
    return $this->render('MyBundle:Counter:test.html.twig', array(
        'data' => $data          
    ));
}

当然,那么JS代码应该是这样的:

$.ajax({  
    type: "POST",  
    url: 'route/to/create'
    data: {firstname:'John'},
    success: function(response)
    {
        console.log(response);
    }
});

我已经对此进行了测试,我认为没有理由不这样做。它对我来说很好......

于 2013-10-07T14:00:42.053 回答
1

请注意这是@EliasVanOotegem 原始示例,但缺少一些明显的步骤

在控制器中,我正在阅读一些回复,如“我无法看到它是如何工作的,因为我正在获取 null”这是因为您没有正确键入您的对象。

IE

var data = { name : 'john' };

$.ajax({
  type: "POST",
  url: url,//post how you get this URL please...
  data: {json : data},//jQ will sort this out for you
  success: function(response)
  {
      console.log(response);
  }
  error: function()
  {
    console.log('an error occured');
    console.log(arguments);//get debugging!
  }
});

正如您现在可以看到访问请求对象一样

$request->get('json');

指 json 数据的 post 键

于 2014-09-18T14:43:50.210 回答
0

内容是您要检索的内容,既不是参数也不是标题。

尝试:

$request->getContent();
于 2013-10-07T14:09:36.057 回答
0

在你的情况下$request->request->get('json')应该这样做。

于 2013-10-07T21:12:47.053 回答