axios param的数据始终post不过去

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>),好了下面开始说正事:


    // Send a POST request
                axios({
                    method: 'post',
                    url: this.url + 'vdata/vdata_cate/createVdataCateProcess',
                    data: {
                     param:JSON.stringify(this.formValidate),//转为json字符串
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                }).then((response) => {

                    //刷新taken 第10步
                    this.getNewToken();

                    if (response.data.r == "success") {
                        swal({
                            type: "success",
                            title: "成功!",
                            text: "添加成功",
                            confirmButtonText: "确认",
                            allowOutsideClick: true,
                            timer: 1300,
                        }, function() {
                            //取消窗口
                        });
                    } else {
                        console.log(response.data);
                        swal({
                            type: "error",
                            title: response.data,
                        });
                    }

                });

param的数据始终post不过去

###

headers 中的 ContentType 和 Payload 冲突

经过 JSON.stringify 转换后是一个 JSON 字符串,但是 application/x-www-form-urlencoded 要求是使用key=value 的形式对参数进行包装,多个参数使用 & 拼接。

解决办法。

  • 1、 使用 QS 包中的 qs.stringify 处理对象。
  • 2、 去掉 JSON.stringifyContent-Type 修改为 application/json
  • 方法2 如果是在 PHP 将无法通过 $_POST 取值,需要使用 file_get_contents('php://input')。在一些框架中,需要使用 Request::getContent 方法
###

看下控制台有报错吗? 然后确认一下Network上的请求的参数没带上?

###

url会不会错了,或者没写端口;刷新token这样子你每次调用都得写,封装下axios然后在拦截器里面写会好很多

###

你是不是搞混了,data里面不用再写param了,应该用下面这一行就行了
data: JSON.stringify(this.formValidate),//转为json字符串

郑重声明:本站部分内容转载自网络,版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们(QQ/微信153890879)修改或删除,多谢。