post,put,patch都比较相似,都可以form-data,applicition/json请求
<template>
<div class="axios"></div>
</template>
<script>
import axios from 'axios'
export default {
name: 'axios2-2',
created() {
/*
post方法:提交数据(表单提交+文件上传)
form-data 表单提交(图片上传,文件上传)
applicition/json
*/
let data = {
id: 12
}
axios.post('/post', data).then(res => {
console.log(res)
})
axios({
method: 'post',
url: '/post', // 虚拟路径
data: data
}).then(res => {
console.log(res)
})
// form-data请求
let formData = new FormData()
for(let key in data){
formData.append(key, data[key])
}
axios.post('/post', formData).then(res => {
console.log(res)
})
},
}
</script>
1、applicition/json请求
// applicition/json请求
let data = {
id: 12
}
axios({
method: 'post',
url: '/post', // 虚拟路径
data: data
}).then(res => {
console.log(res)
})
2、form-data请求
let data = {
id: 12
}
// form-data请求
let formData = new FormData()
for(let key in data){
formData.append(key, data[key])
}
axios.post('/post', formData).then(res => {
console.log(res)
})
put请求
// put请求
let data = {
id: 12
}
axios.put('/put', data).then(res => {
console.log(res)
})
patch请求
// patch请求
let data = {
id: 12
}
axios.patch('/patch', data).then(res => {
console.log(res)
})