vue axios用post提交的数据格式

post提交数据的四种编码方式

1.application/x-www-form-urlencoded 
这应该是最常见的post编码方式,一般的表单提交默认以此方式提交。大部分服务器语言对这种方式都有很好的支持。在PHP中,可以用$_POST[“key”]的方式获取到key的值,在node中我们可以使用querystring中间件对参数进行分离 

  1. app.post("/server",function(req,res){

  2. req.on("data",function(data){

  3. let key=querystring.parse(decodeURIComponent(data)).key;

  4. console.log("querystring:"+key)

2.multipart/form-data 
这也是一种比较常见的post数据格式,我们用表单上传文件时,必须使form表单的enctype属性或者ajax的contentType参数等于multipart/form-data。使用这种编码格式时发送到后台的数据长得像这样子 
这里写图片描述 
不同字段以--boundary开始,接着是内容描述信息,最后是字段具体内容。如果传输的是文件,还要包含文件名和文件类型信息

3.application/json 
axios默认提交就是使用这种格式。如果使用这种编码方式,那么传递到后台的将是序列化后的json字符串。我们可以将application/json与application/x-www-form-urlencoded发送的数据进行比较 
首先是application/json: 
这里写图片描述 
接着是application/x-www-form-urlencoded: 
这里写图片描述 
这里可以明显看出application/x-www-form-urlencoded上传到后台的数据是以key-value形式进行组织的,而application/json则直接是个json字符串。如果在处理application/json时后台还是采用对付application/x-www-form-urlencoded的方式将会产生问题。例如后台node.js依然采用之前对付application/x-www-form-urlencoded的方法,那么querystring.parse(decodeURIComponent(data))之后得到的数据是这样子的 
这里写图片描述 
这个时候再querystring.parse(decodeURIComponent(data)).key只能获取到undefined

4.text/xml 
剩下的一种编码格式是text/xml,这种格式我没有怎么使用过

解决方法

既然我们知道axios post方法默认使用application/json格式编码数据,那么解决方案就有两种,一是后台改变接收参数的方法,另一种则是将axios post方法的编码格式修改为application/x-www-form-urlencoded,这样就不需要后台做什么修改了。 
先来看第一种解决方法 
vue组件中,axios发送post请求的代码如下 

  1. this.$axios({

  2. method:"post",

    扫描二维码关注公众号,回复: 6150919 查看本文章
  3. url:"/api/haveUser",

  4. data:{

  5. name:this.name,

  6. password:this.password

  7. }

  8. }).then((res)=>{

  9. console.log(res.data);

  10. })

此时控制台Network Headers里面的信息是这样子的 
这里写图片描述 
后台接收数据需要依赖body-parser中间件,我们事先装好,接着在后台代码中引用body-parser 
这里写图片描述 
这张截图中,发挥作用的代码仅仅是const bodyParser=require("body-parser"); 
接下来在路由中使用body-parser 

  1. app.post("/api/haveUser",bodyParser.json(),function(req,res){

  2. console.log(req.body);

  3. let haveUser=require("../api/server/user.js");

  4. haveUser(req.body.name,req.body.password,res);

  5. });

这时,当前台发送post请求之后,后台控制台中就会打印出req.body 
这里写图片描述 
这时,通过req.body.name或者req.body.password就能拿到对应的值。 
这种方法比较简单,也不需要前台做过多修改,推荐使用这种方法。

第二种解决方法,具体操作如下 
前端 

  1. this.$axios({

  2. method:"post",

  3. url:"/api/haveUser",

  4. headers:{

  5. 'Content-type': 'application/x-www-form-urlencoded'

  6. },

  7. data:{

  8. name:this.name,

  9. password:this.password

  10. },

  11. transformRequest: [function (data) {

  12. let ret = ''

  13. for (let it in data) {

  14. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'

  15. }

  16. return ret

  17. }],

  18. }).then((res)=>{

  19. console.log(res.data);

  20. })

其中发挥关键作用的是headers与transformRequest。其中 headers 是设置即将被发送的自定义请求头。 transformRequest 允许在向服务器发送前,修改请求数据。这样操作之后,后台querystring.parse(decodeURIComponent(data))获取到的就是类似于{ name: 'w', password: 'w' }的对象。后台代码如下 

  1. app.post("/api/haveUser",function(req,res){

  2. let haveUser=require("../api/server/user.js");

  3. req.on("data",function(data){

  4. let name=querystring.parse(decodeURIComponent(data)).name;

  5. let password=querystring.parse(decodeURIComponent(data)).password;

  6. console.log(name,password)

  7. haveUser(name,password,res);

  8. });

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

猜你喜欢

转载自blog.csdn.net/hsany330/article/details/89468055