post发送json与表单的post请求--后台无法接收到数据

个人blog,欢迎关注加收藏

后台定义需要formData格式的数据:即通过表单提交post请求传送的数据

前端用ajax的post请求传送的数据后台无法接收(后台接收的数据对象为空):
错误代码如下:
//买卖EMT操作
operateEMTF(operate){
let _this = this;
let api = "order/place";
let temp = {
	type:“buy-limit”,
	price:0.8763,
	quantity:1000
};
console.log(temp);
_this.axios.post(api,{
type:temp.type,
price:temp.price.toString(),
quantity:temp.quantity.toString()
}).then(res=>{
// console.log(temp,res);
_this.buy.price = "";
_this.buy.quantity = "";
_this.sell.price = "";
_this.sell.quantity = "";
});
},


















解决方案一:前端解决
必须把参数temp的json格式转化为formData格式:
代码如下:

//买卖EMT操作
        operateEMTF(operate){
            let _this = this;
            let api = "order/place";
            let temp = {};
            if(operate=="buy"){
                temp = _this.buy;
            }else{
                temp = _this.sell;
            }
            console.log(temp);
			//1.json格式转化formData格式
            let fd = new FormData();
            Object.keys(temp).forEach(key => fd.append(key, temp[key]))

            _this.axios({
            method: 'post',
            url: api,
            data: fd,
			//2.在请求头中制定参数格式为formData格式
            config: { headers: {'Content-Type': 'multipart/form-data' }}
            })
            .then(function (response) {
                console.log(response);
            })
            .catch(function (response) {
                console.log(response);
            });



			//2.或者简写如下:
_this.axios.post(api,fd,{
                headers: {'Content-Type': 'multipart/form-data' }
            }).then(res=>{
                console.log(res);
            });

        },




解决方案二:后台解决
后台把request.body用json.loads转一下


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值