axios中delete请求方式的注意点

axios一共有四种请求方式分别为,get,post, put,delete。

get:一般用于从服务器获取数据。此请求一般不包含请求体,只有请求参数,请求参数可以作为url的一部分去传递,也可以通过params传递

示例

// 通过url本身传递
axios.get('https://api.example.com/resource?id=123')
    .then(response => console.log(response))
    .catch(e => console.log(e))

// 通过params传递
axios.get('https://api.example.com', {params: {id : 123}})
    .then(response => console.log(response))
    .catch(e => console.log(e))

post:一般用于向服务器提交,请求参数通常通过请求体进行传递。你可以将参数作为post方法的第二个参数传递,或者使用data选项将参数传递给post方法

// 将参数作为 post 方法的第二个参数传递
axios.post('https://api.example.com/resource', { param1: 'value1', param2: 'value2' })
  .then(response => console.log(response))
  .catch(e => console.log(e));

// 使用 data 选项传递参数
axios.post('https://api.example.com/resource', null, { data: { param1: 'value1', param2: 'value2' } })
  .then(response => console.log(response))
  .catch(e => console.log(e));

 put:通常用于更新,修改已存在的资源,请求参数传递与请求体传递一致

axios.put('https://api.example.com/resource', { param1: 'value1', param2: 'value2' })
  .then(response => console.log(response))
  .catch(e=> console.log(e));

delete: 用于删除已有资源,一般不通过请求体来传递参数,作为查询参数传递,通过params选项进行配置。

axios.delete('https://api.example.com/resource', { params: { param1: 'value1', param2: 'value2' } })
  .then(response => console.log(response))
  .catch(e => console.log(e));

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值