axios获取请求参数的方法--delete篇

第一种方法(一般用这种):把参数拼接在url地址栏上

const deleteNews = async (i) => {
  console.log(i)
  const res = await axios.delete('/news/delete', {
    params: { index: i }
  })
  data.list = res
}
const handleDeleteer = (opt) => {
  console.log(opt.url)   //  news/delete?index=2
  const index = opt.url.split('?')[1].slice(6)
  arr.splice(index, 1)
  return arr
}

//delete请求的查询参数拼接在请求路径中的,需要对路径进行处理(改成正则)
Mock.mock(/^\/news\/delete/, 'delete', handleDeleteer)

第二种方法:把参数写在请求体中:将params改为 data

const deleteNews = async (i) => {
   console.log(i)
   const res = await axios.delete('/news/delete', {
     data: { index: i }
  })
  data.list = res
}
const handleDelete = (opt) => {
  console.log(opt)  // {url: '/news/delete', type: 'DELETE', body: '{"index":4}'}
  console.log(opt.body)  //{"index":4}   //字符串
  let { index } = JSON.parse(opt.body)  //转为对象,然后解构
  console.log(index)   //4
  arr.splice(index, 1)
  return arr
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容