javascript - Axios POST 请求在 localhost 上工作,但在 Netlify 上部署后不再工作
问题描述
axios POST 请求在我的本地主机上运行良好,但在 Netlify 上部署我的站点后不再运行。
我收到的错误是:
由于错误未发送消息:在 XMLHttpRequest.handleError (webpack:///./node_modules/axios/) 的 createError (webpack:///./node_modules/axios/lib/core/createError.js?:16) lib/适配器/xhr.js?:81)**
我是否在请求中输入了错误的 url?
已经尝试在我的服务器上添加 CORS。
我在做什么错:(请帮忙!!!!
前端的 POST 请求:
formSubmit = e => {
e.preventDefault();
this.setState({
buttonText: "...sending"
});
let data = {
name: this.state.name,
email: this.state.email,
phone: this.state.phone,
message: this.state.message
};
axios
.post("https://www.thejunkim.com/sendmsg", data)
.then(res => {
this.setState({ sent: true }, this.resetForm());
})
.catch(err => {
console.log("Message not sent because of ", err);
});
};
服务器:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.post("/sendmsg", (req, res, next) => {
let data = req.body;
let mailOptions = {
from: '"Nodemailer Contact" <hjk013@gmail.com>',
to: "hjk013@gmail.com",
subject: "SOMEONE SENT MSG FROM YOUR PERSONAL WEBSITE",
html: output
};
transporter.sendMail(mailOptions, (err, res) => {
if (err) {
console.log(err);
} else {
// console.log(JSON.stringify(res));
console.log("Message sent: %s", res.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(res));
console.log("res", res);
res.end();
}
});
res.end();
});
解决方案
推荐阅读
- lua - Openresty 和 Lua 的性能分析
- python - 使用 pandas lib 打开 gnumeric
- ruby-on-rails - 错误:列“name_id”无法自动转换为整数类型您可能需要指定“USING name_id::integer”
- c# - LINQ 可枚举查询是否返回副本或引用?
- reactjs - 我可以使用 React Native 围绕 Web 应用创建移动应用包装器,类似于 Cordova 创建移动应用的做法吗?
- markdown - 将 MathJax 与 kramdown 一起使用,而不在 div 中进行保护(如宣传的那样!)
- ag-grid - ag-grid:开始编辑树视图的 autoColumnDef
- r - 与 x 轴刻度作斗争
- php - Laravel 7多选一对多关系
- python - 大型模型的最佳批量大小和时期