How can I send JSON response in symfony2 controller

后端 未结 5 1755
栀梦
栀梦 2020-12-04 11:02

I am using jQuery to edit my form which is built in Symfony.

I am showing the form in jQuery dialog and then submitting it.

5条回答
  •  醉话见心
    2020-12-04 11:36

    Since Symfony 3.1 you can use JSON Helper http://symfony.com/doc/current/book/controller.html#json-helper

    public function indexAction()
    {
    // returns '{"username":"jane.doe"}' and sets the proper Content-Type header
    return $this->json(array('username' => 'jane.doe'));
    
    // the shortcut defines three optional arguments
    // return $this->json($data, $status = 200, $headers = array(), $context = array());
    }
    

提交回复
热议问题