axios的请求方法:get、post、put、patch、delete
get:获取数据
post:提交数据(表单提交+文件上传)
put:更新数据(所有数据推送到后端)
patch:更新数据(只将更改的数据推送到后端)
delete:删除数据**

//axios的get请求第一种写法不带参数
axios.get(’/data.json’).then((res)=>{undefined
console.log(res)
}),

//axios的get请求第一种写法带参数
axios.get(’/data.json’,{undefined
params:{undefined
id:12
}
}).then((res)=>{undefined
console.log(res)
}),

//axios的get请求第二种写法不带参数
axios({undefined
method:‘get’,
url:’/data.json’,
}).then(res=>{undefined
console.log(res)
}),

//axios的get请求第二种写法带参数
axios({undefined
method:‘get’,
url:’/data.json’,
params:{undefined
id:12
},
}).then(res=>{undefined
console.log(res)
}),

//axios的post请求第一种写法
let data = {undefined
id:12
}
axios.post(’/post’,data).then((res)=>{undefined
console.log(res)
}),

//axios的post请求第二种写法
axios({undefined
method:‘post’,
url:’/post’,
data:data
}).then(res=>{undefined
console.log(res)
}),

//form-data请求,图片上传、文件上传,文件格式为:multipart/form-data,其他请求为application/json

let formData = new formData()
for(let key in data){undefined
formData.append(key,data[key])
},
axios.post(’/post’,formData).then(res=>{undefined
console.log(res)
})

//axios之put请求
axios.put(’/put’,data).then(res=>{undefined
console.log(res)
})

//axios之patch请求
axios.patch(’/patch’,data).then(res=>{undefined
console.log(res)
}),

//axios之delete请求的第一种写法
axios.delete(’/delete’,{undefined
params:{undefined
id:12
}
}).then(res=>{undefined
console.log(res)
})
//说明:当使用第一种写法参数为params时,请求接口时参数是放在URL里面的。
// 例:http://localhost:8080/delete?id=12,而写成第二种方法data就不会,根据实际情况使用

//axios之delete请求的第二种写法
axios.delete(’/delete’,{undefined
data:{undefined
id:12
}
}).then(res=>{undefined
console.log(res)
})
————————————————
版权声明:本文为CSDN博主「乔7426」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qiaoguoqing/article/details/100077724

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐