1.GET:用域获取数据
2.POST:用于提交数据
3.PUT :更新数据(吧所有数据推送到后端)
4.PATCH:更新数据(只推送修改的数据到后端)
5.DELETE:删除数据
一个简单的请求
axios({
method:'GET',
url:'/data.json',
params:{
//传递的参数
id:12
//在URL中为
//http://localhost:8080/data.json?id=12
}
})
.then(res=>{
console.log(res);
})
.catch(err=>{
console.log(err);
})
//当然这个也可以这样写
/*let request=axios.create({
baseURL:"http://localhost:8080",
timeout:1000
})
request.get("/data.json").then.....*/
POST方法提交
axios({
method:'POST',
url:'/post',
data:data
//提交的数据用data表示
})
.then(res=>{
console.log(`res ->${res}`);
})
.catch(err=>{
console.log(err);
})
PUT请求和PATCH请求
axios({
method:'PUT',
url:'/put'
})
.then(res=>{
console.log(`res ->${res}`);
})
.catch(err=>{
console.log(err);
})
axios({
method:'PATCH',
url:'/patch'
})
.then(res=>{
console.log(`res ->${res}`);
})
.catch(err=>{
console.log(err);
})
这里params和data的区别就是params是在URL进行参数传递的,而data不是