【问题标题】:Axios delete and put requests - 404 erroraxios删除和放置请求 - 404错误
【发布时间】:2021-12-07 03:43:09
【问题描述】:

我正在尝试通过 axios 向我的 api 发送删除和放置请求。 使用 post/get 一切正常,当我尝试使用邮递员发送 delete/put 时,一切都很好,所以问题出在 react 应用程序上。

axios 实例:

const api = Axios.create({
  baseURL: "http://localhost:8000",
  timeout: 1000,
  headers: {'Content-Type': 'application/json'}
});

请求:

Axios.delete("/",{index:name})
    .then((response)=>{console.log(response)})
    .catch((err)=>{console.log(err)});

*put是一样的,用另一个数据

每当我部署这些代码时,我都会在控制台中收到 404 错误,并且服务器没有收到请求。

【问题讨论】:

    标签: reactjs api axios


    【解决方案1】:

    您没有使用通过正确的 baseURL 创建的 axios 实例。将您的请求更改为:

    api
      .delete("/",{index:name})
      .then((response)=>{console.log(response)})
      .catch((err)=>{console.log(err)});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-24
      • 2019-12-06
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      相关资源
      最近更新 更多