最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Axios delete method with a payload - Stack Overflow

programmeradmin0浏览0评论

Im using axios on my react app to send a delete request, I need to send an id list as payload but it returns "415 Unsupported Media Type".

Here's my code:

const deviceData = ["31234"];

axios.delete(url, { data: deviceData }).then(res => {
  if (res.status === 200) {
    const pagination = { ...this.state.pagination };
    this.setState({
      loading: false,
      data: res.data.data.devices,
      pagination
    });
  }
});

Im using axios on my react app to send a delete request, I need to send an id list as payload but it returns "415 Unsupported Media Type".

Here's my code:

const deviceData = ["31234"];

axios.delete(url, { data: deviceData }).then(res => {
  if (res.status === 200) {
    const pagination = { ...this.state.pagination };
    this.setState({
      loading: false,
      data: res.data.data.devices,
      pagination
    });
  }
});

Share Improve this question edited Oct 4, 2019 at 9:37 Joshua 3,1763 gold badges26 silver badges40 bronze badges asked Oct 4, 2019 at 9:29 SNoVSNoV 971 gold badge2 silver badges11 bronze badges 4
  • the problem only with delete method ? – Rio A.P Commented Oct 4, 2019 at 9:56
  • @RapSherlock Yes – SNoV Commented Oct 4, 2019 at 10:49
  • 1 can you try to debug your request in back-end ? – Rio A.P Commented Oct 4, 2019 at 10:56
  • @RapSherlock request is tested in postman and its working. – SNoV Commented Oct 4, 2019 at 14:11
Add a comment  | 

1 Answer 1

Reset to default 17

axiox.delete does support a request body. It accepts two parameters: url and optional config. You can use config.data to set the response body as follows:

axios.delete(url, { data: { foo: "bar" } });

See here to more information: https://github.com/axios/axios/issues/897#issuecomment-343715381

Or you can try to set header with: 'Content-Type': 'application/json; charset=utf-8'

const deviceData = ["31234"];

axios.delete(url,
  { headers:{'Content-Type': 'application/json; charset=utf-8'} },
  { data: { deviceData: deviceData } }).then(res => {
  if (res.status === 200) {
    const pagination = { ...this.state.pagination };
    this.setState({
      loading: false,
      data: res.data.data.devices,
      pagination
    });
  }
});
发布评论

评论列表(0)

  1. 暂无评论