1 POST 请求
1.1 基本表单上传
默认为 JSON 数据格式,因此上传 JSON 数据时不需要设置请求头;
axios.post(url, params)
.then(function (response) {
console.log('请求成功')
})
.catch(function (error) {
console.log('请求失败', error)
})
1.2 文件上传
由于文件上传不是 JSON 数据格式,所以需要设置请求头;
const data = new FormData();
// 如需要同时上传其它数据,也在此处进行添加
data.append('file', file);
axios.post('http://localhost:3006/common/upload', data, {
headers: { 'Content-Type': 'multipart/form-data' }
}).then(res => {
console.log(res);
}).catch(err => {
console.log(err);
});
2 GET 请求
axios.get(url)
.then(function (response) {
console.log('请求成功')
})
.catch(function (error) {
console.log('请求失败', error)
})