Axios POST请求可在localhost上运行,但在Netlify上部署后将不再可用

时间:2019-06-12 00:42:38

标签: javascript reactjs axios netlify

axios POST请求在我的本地主机上工作正常,但是在Netlify上部署我的站点后不再起作用。

我收到的错误是:

  

由于错误而未发送消息:网络错误       在createError(webpack:///./node_modules/axios/lib/core/createError.js?:16)       在XMLHttpRequest.handleError(webpack:///./node_modules/axios/lib/adapters/xhr.js?:81)**

我在请求中输入了错误的网址吗?

已经尝试在我的服务器上添加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();
});

0 个答案:

没有答案