【问题标题】:Axios POST 404 not found未找到 Axios POST 404
【发布时间】:2020-01-16 11:50:16
【问题描述】:

我想在 react 中发送带有表单的电子邮件并将正文发送到节点我使用 axios.post 这是我的代码:

handleSubmit = async e => {
e.preventDefault();

const { name, number, email, message } = this.state;

await axios.post("/send", { name, number, email, message });

console.log(this.state);
};

错误信息是:

POST http://localhost:3000/send 404 (Not Found)
createError.js:17 Uncaught (in promise) Error: Request failed with status code 404

不知道为什么

【问题讨论】:

  • "/send" 指向您的前端服务器,而不是后端服务器
  • 我必须做什么
  • 您需要发布到后端的完整 URL 而不是相对路径,或者在配置对象中传递 baseURL 属性(.post() 的第三个参数)

标签: reactjs email axios


【解决方案1】:

你必须设置你的后端服务器 baseUrl 你可以这样做

 await axios.post("baseUrl/send", { name, number, email, message });

或者您可以创建 axios 实例并设置您的基本 url 并使用您的实例发送请求

const instance = axios.create({
   baseURL: 'https://some-domain.com/api/',
   timeout: 1000,
   headers: {'X-Custom-Header': 'foobar'}
});

然后使用这个实例发送请求

【讨论】:

    猜你喜欢
    • 2020-01-22
    • 2021-10-07
    • 2016-10-11
    • 1970-01-01
    • 2022-01-23
    • 2014-09-09
    • 2014-10-24
    • 2018-05-10
    • 1970-01-01
    相关资源
    最近更新 更多