当前位置: 首页>后端>正文

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
}

https://www.xamrdz.com/backend/3ae1936647.html

相关文章: