首页 > 解决方案 > 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();
});

标签: javascriptreactjsaxiosnetlify

解决方案


推荐阅读