axios的几种请求接口方式

axios的请求方法:get、post、put、patch、delete

get:获取数据

post:提交数据(表单提交+文件上传)

put:更新数据(所有数据推送到后端)

patch:更新数据(只将更改的数据推送到后端)

delete:删除数据

 

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

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

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

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

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

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

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

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

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

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

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

//axios之delete请求的第二种写法
axios.delete('/delete',{
  data:{
    id:12
  }
}).then(res=>{
  console.log(res)
})

  • 6
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值