从POST请求中获取数据

时间:2018-02-24 18:32:43

标签: ajax vue.js symfony3.x

我正在尝试使用VueJS-Ressource的AJAX请求更新我的用户实体的一些数据。

这是我的AJAX请求:

           this.$http.post('/profile/edit', {
               description: this.description
           }).then(response => {
               console.log(response);
           }, response => {
               console.log(response);
           });

这是我收到此AJAX请求的控制器:

/**
 * @Route("/edit", name="profile_edit")
 * @Method({"POST"})
 */
public function profileEditAction(Request $request) {
    if ($request->isXmlHttpRequest()) {
        /* @var User $user */
        $user = $this->getUser();
        $description = $request->request->get( 'description');

        if (isset($description)) {
            $user->setDescription($description);
            $this->getDoctrine()->getManager()->flush($user);
            return new Response('Updated User description with success !');
        }

        return new Response('No description parameter found');
    }
    return $this->redirectToRoute('homepage');
}

我的问题是,每当我尝试使用$request->request->get('PARAMETER_NAME')从我的请求中获取参数时,它都会返回一个空值。

我从浏览器显示我的请求,但我清楚地看到我正在发送内容:

完整的AJAX请求:https://i.gyazo.com/825ea438b09e4df8d8287555a1c841a6.png

我希望有人可以帮助我,谢谢你!

1 个答案:

答案 0 :(得分:0)

尝试将数据发布为 FromData (以便将Content-Type作为multipart/form-data发送)。

在VueJS中,请执行以下操作:

var formData = new FormData();
formData.append('description', this.description);

this.$http.post('/profile/edit', formData)
    .then(response => {
        console.log(response);
    }, reason => {
        console.log(reason);
    });

确保开发工具 - >网络您将发送的数据视为表单数据(而不是请求有效负载)。

在服务器上执行var_dump( $request->request->all() )并查看request包含的内容。