带有axios的POST请求未发送参数 [英] POST Requests with axios not sending parameters

查看:519
本文介绍了带有axios的POST请求未发送参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将一些数据从Vue.js发布到基于Symfony的后端.

I am trying to POST some data from Vue.js to a backend based on Symfony using the following code.

       updateQuestion : function() {
            axios.post('/staff/question/api/' + this.id,{
                    id : 'test',
                    name : 'sree'
            })
                .then( response => {

                console.log(response);
            })
                .catch(error => {
                    console.log(error);
                })
        },

但是,我附加到POST请求的参数没有到达我的控制器.因此,我尝试了POST请求的备用格式,但参数仍未到达控制器.请告诉我怎么了.

However, the parameters that I am attaching to the POST request are not reaching my controller. So, I tried the alternate format for POST requests and still, the parameters are not reaching the controller. Please tell me what's wrong.

备用格式:

 updateQuestion : function() {
            axios({

                method : 'POST',
                url : '/staff/question/api/' + this.id,
                data: {
                    id : 'test',
                    name : 'sree'
                }

            })
                .then( response => {

                console.log(response);
            })
                .catch(error => {
                    console.log(error);
                })
        },

推荐答案

我也遇到了这个问题! 我的帖子数据在控制器中找到:

I also encountered this problem! My post data was found in the controller:

$request->getContent();

我的Vue脚本

onSubmit() {
  axios.post('/test/post/data', { test: "test" })
    .then(response => {
      console.log(response.data);
    });
},

我的Symfony控制器:

My Symfony controller:

public function postData(Request $request)
{
    $data = $request->getContent();
    $data = json_decode($data, true);

    return $this->json($data);
}

这篇关于带有axios的POST请求未发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆