我有node.js应用程序,单击该按钮即可发送发帖请求,并在发帖请求后将电子邮件发送给管理员。我发起请求的页面链接可在以下位置找到:https://blockmmerce.io/views/contact。
单击按钮后,将立即调用事件侦听器,但它将尝试发送请求并返回502错误代码。
任何可能的解决方法?
contact.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));
}
});
});
});
Index.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;
email.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
}
答案 0 :(得分:0)
您必须添加内容类型并以字符串形式提交数据
$.ajax({
contentType: 'application/json',
data: JSON.stringify({
name:"Bob",
...
})
});
此外,您还必须从服务器端向用户返回响应,也不要使用await并立即将响应返回给用户,无需等待邮件提交,因为这会花费时间。
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({});
});