0

我有 node.js 应用程序,单击按钮发送发布请求并在发布请求时将电子邮件发送给管理员。我发起请求的页面链接可以在这里找到:https ://blockmmerce.io/views/contact 。

一旦我单击按钮,就会调用事件侦听器,但它会尝试发送请求并返回 502 错误代码。

任何可能的修复?

联系人.js:

$(document).ready(function () {
console.log("here...")
var enquirybutton = document.querySelector('#sendEnquiry');
enquirybutton.addEventListener('click', function (event) {
    alert("clicked!!");
    let name = $('#name').val();
    let email = $('#email').val();
    let message = $('#message').val();
    event.preventDefault();
    $.ajax({
          url: "/sendEnquiry",
          dataType: "json",
          type: "post",
          cache: false,
          data: ({
            "name": name,
            "email": email,
            "message": message
          }),
          success: function (response) {
            alert("Enquiry sent successfully !!!")
          },
          error: function (textStatus, errorThrown) {
            console.log("Error::::" + errorThrown);
            console.log("status::::" + String(textStatus));
          }
        }); 
});

});

索引.js:

    var express = require('express');
var router = express.Router();
var emailConfig = require('../config/email.config.js');

router.post('/sendEnquiry', function (req, res, next){
    let name = req.body.name;
    let email = req.body.email;
    let message = req.body.message;
    emailConfig.sendEmail(
        'admin@blockmmerce.io',
        'Enquiry',
        `this is first query`
    );
    res.json({});
    console.log("hello from send enquiry");
});

modules.exports = export;

电子邮件.config.js:

var nodemailer = require('nodemailer');

//--------------------------------------------------------
// Declarations    
//--------------------------------------------------------
var senderEmail = 'info@server119.web-hosting.com';
// var password = '----';
//--------------------------------
// Mail Sending Email
//--------------------------------------------------------
var transporter = nodemailer.createTransport({
    host: 'server119-3.web-hosting.com',
    secure: true,
    port: 465,
    auth: {
        user: 'info@blockmmerce.io',
        pass: '------'
    }
});

// ================================================================
// handle Sendmail
// ================================================================

var sendEMail = (emailId, subject, mailcontent) => {
    let mailOptions = {
        from: senderEmail,
        to: emailId,
        subject: subject,
        html: mailcontent
    };
    return new Promise(function (resolve, reject) {
        transporter.sendMail(mailOptions, function (err, info) {
            if (err) {
                reject(err);
            } else {
                resolve(info);
            }
        });

    });
}

//--------------------------------------------------------
// Exporting All functions
//--------------------------------------------------------

module.exports = {
    transporter,
    senderEmail,
    sendEMail
}
4

1 回答 1

0

您必须添加内容类型并将数据作为字符串提交

$.ajax({
    contentType: 'application/json',
    data: JSON.stringify({
        name:"Bob",
        ...
    })
});

此外,您必须从服务器端向用户返回响应,也不要使用等待并立即向用户返回响应,无需等到邮件提交,因为这需要时间。

router.post('/sendEnquiry', function (req, res, next){
 let name = req.body.name;
 let email = req.body.email;
 let message = req.body.message;
 emailConfig.sendEmail(
     'admin@blockmmerce.io',
     'Enquiry',
    // `Hi, I have an enquiry that: ${message}. You can reply back  on my this email ${email}. Thanks, ${name}`
    `${message}`
  );
res.json({});

});
于 2019-08-24T14:59:03.417 回答