我想在 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
不知道为什么
最佳答案
你必须设置你的后端服务器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'}
});
然后使用该实例发送请求
关于reactjs - Axios POST 404 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57943335/