axios里的post请求时转换Request Payload 和formData

axios进行前后端访问时会自动将起序列话。

axios默认的格式是Request Payload

而如果后台使用Httpservlet时使用request.getParameter时只能拿到格式为formData的数据,这是就需要进行数据转换。

你也可以在引入axios时就设置默认的格式:

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'

我试了一下只设置这个是不行的。还要

axios.post('http://localhost:3000/api/goods/get',qs.stringify(data),{
        headers: {
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'
          }
      }).then(res=>{
        _this.msg = res.data
      },err =>{
        console.log(err)
      })

其中qs模块是一件封装进axios模块里了,

引入axios时同时引入qs即可。

如:

1

2

import axios from 'axios'

import qs from 'qs'

猜你喜欢

转载自blog.csdn.net/margin_0px/article/details/84345561