axios post请求-FormData上传文件

说明

可以实现 表单方式提交 的效果,后端代码可以用 request.getParameter() 方式接收参数

而且可以 上传文件

post发送数据方式: Content-Type: multipart/form-data

前端代码

<script src="https://cdn.staticfile.org/axios/0.20.0/axios.min.js"></script>

<script>

let formData = new FormData();
formData.append("username", "lilei");
formData.append("password", "123456")
formData.append('file', this.file);


var config = {
    headers: {
        'Content-Type': 'multipart/form-data'
    }
};

axios.post('/addUser', formData, config)
  .then(function (res) {
    console.log(res);
  })
  .catch(function (error) {// 请求失败处理
    console.log(error);
  });
</script>

发送头信息

axios.post('http://localhost:8080/web_war_exploded/testJWT', '',{
    headers: {
        'auth': localStorage.getItem('auth')
    }
})
.then(res => {
    console.log(res);
})
.catch(erro => {// 请求失败处理
    console.log(error);
});

原文出处:https://www.malaoshi.top/show_1IX432ffakyd.html