簡體   English   中英

無法獲取 ajax 表單數據以使用 nodemailer 提交

[英]Cant get ajax form data to submit with nodemailer

大家早上好,我已經使用 node.js 和快遞制作了 web 應用程序。 我讓 Nodemailer 發送 email 而我的 AJAX 正在發送解析的 JSON 數據來表達,但我無法從數據中獲取到 nodemailer。 我的 Ajax 正在發送 JSON 來表達我已經用 DEV 工具確認了這一點,但是我不知道如何將 JSON 放入 nodemailer。 任何幫助將非常感激。

 /* contact Route: contact.js */ var express = require('express'); const contact = express.Router(); var path = require('path'); const bodyParser = require('body-parser'); var nodemailer = require('nodemailer'); contact.use(bodyParser.json() ); contact.use(express.static(__dirname + 'portfolio')); contact.get('/contact', (req,res,next) => { res.sendFile(path.join(__dirname, '../html-files', 'contact.html')); console.log('this works'); }); contact.post('/contact', (req,res) => { /*const data = req.body.data; const from = data.email; const text = data.message;*/ var transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: 'augustshah@02pilot.com', pass: 'hgahalzecelxdxis' } }); var mailOptions = { from: this.email, to: 'augustshah@02pilot.com', subject: 'Quote', text: this.message }; transporter.sendMail(mailOptions, function(error, info){ if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } }); }) module.exports = contact; /* Jquery: script.js*/ //const { json } = require("body-parser"); //var requirejs = require('requirejs'); //const { json } = require("body-parser"); $('#submit').on('click', function(e) { e.preventDefault(); const name = $("#name").val(); const email = $("#email").val(); const message = $("#message").val(); var $form = $( this ), url = $form.attr( "action", "/contact"); const data = { name: name, email: email, message: message }; $.ajax({ type: "POST", // HTTP method POST or GET url: "/contact", //Where to make Ajax calls dataType: "json", // Data type, HTML, json etc. data: JSON.stringify(data), //Form variables success: function() { alert("Your Email has been sent"); }, error: function() { alert("Your Email has not sent. Try Again. "); } }) });

修正:這很簡單。 在我的 script.js 中,我沒有設置 contentType。 我將其設置為“應用程序/json”並解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM