详解vue axios用post提交的数据格式

2020-06-13 10:38:19易采站长站整理
headers
是设置即将被发送的自定义请求头。
transformRequest
允许在向服务器发送前,修改请求数据。这样操作之后,后台
querystring.parse(decodeURIComponent(data))
获取到的就是类似于
{ name: 'w', password: 'w' }
的对象。后台代码如下


app.post("/api/haveUser",function(req,res){
let haveUser=require("../api/server/user.js");
req.on("data",function(data){
let name=querystring.parse(decodeURIComponent(data)).name;
let password=querystring.parse(decodeURIComponent(data)).password;
console.log(name,password)
haveUser(name,password,res);
});
});

这种方法明显就要比第一种麻烦一点,但不需要后台做过多处理。所以具体操作还是得根据实际情况决定。