axios delete方法怎么把参数放在body中

Neoo1984
  • 53
export function deleteFactory(factoryId) {
  return request({
    url: `factory/delete`,
    method: 'delete',
    data:{factoryId}
  })
}

request是封装的一个axios实例

    handleDelete($index, row) {
      const id = row.factoryId
      deleteFactory(id).then(res=>{
        console.log(res.data.success)
      })

    },

看到delete方法第二个参数是config,不懂这个config是什么意思
swagger中的调试
image.png

回复
阅读 1.6k
1 个回答

放在body中简单,但要区分参数类型,比如是FormData还是json等

var axios = require('axios');
var qs = require('qs');

//1
var config = {
  method: 'delete',
  url: 'xxx',
  headers: { 
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data : qs.stringify({'a': '1','b': '2' })
};

//2
var config = {
  method: 'delete',
  url: 'xxx',
  headers: { 
    'Content-Type': 'application/json'
  },
  data : JSON.stringify({"a":1,"b":2})
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});
宣传栏