【问题标题】:I got aborted request in my post request using axios我使用 axios 在我的发布请求中中止了请求
【发布时间】:2020-06-17 14:44:47
【问题描述】:

我正在尝试发帖请求,但我总是收到错误:

Error: "Request aborted"
    createError createError.js:16
    handleAbort xhr.js:73

   [HMR] Waiting for update signal from WDS...   log.js:24

我在这段代码中使用 react.js,我是新手。无论有没有我的后端,我都会收到这个错误,所以我相信我的错误在前面。另外,我已经测试了我的后端,所有路由都运行良好。下面给出了我的发布请求以及我的 axios 配置文件。你们能帮帮我吗?我已经尝试过以下代码的一些变体,例如,我尝试在 axios 中仅将 /cadastro 中的 URL 作为 URL 发送,而不是整个路径。

发布请求:

  const [nome, setNome] = useState("");
  const [email, setEmail] = useState("");
  const [cpf, setCpf] = useState("");
  const [senha, setSenha] = useState("");
  const [rg, setRg] = useState("");
  const [data_nascimento, setDataNascimento] = useState("");

  async function save(){

    try{
      await api.post('http://localhost:5000/cadastro', {
        nome,
        cpf,
        email,
        rg,
        data_nascimento,
        senha
      })
    }catch(err){
      console.log(err)
    }
  }

axios:

import Axios from 'axios';

const api = Axios.create({
  baseURL: 'localhost:5000',
  headers: {
    "Content-Type": "application/json",
  },
  crossdomain: true
});

api.interceptors.request.use(function (config) {
  // Do something before request is sent
  console.log(config)
  return config;
}, function (error) {
  console.log(error)
  return Promise.reject(error);
});

export default api;

编辑 1:我尝试了 ludwiger 告诉我的操作,但我得到了同样的错误。

【问题讨论】:

  • 你可以尝试将http://添加到baseUrl

标签: reactjs post axios


【解决方案1】:

如果您在配置中设置baseUrl,那么您需要在调用中使用相对路径

await api.post('/cadastro', {...

【讨论】:

    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 2019-03-19
    • 2020-09-24
    • 1970-01-01
    • 2021-09-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多