【问题标题】:Cant get ajax form data to submit with nodemailer无法获取 ajax 表单数据以使用 nodemailer 提交
【发布时间】:2020-11-24 16:27:47
【问题描述】:

大家早上好, 我使用 node.js 和 express 制作了一个网络应用程序。我让 Nodemailer 发送电子邮件,而我的 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. ");
        }
    
    })

    
}); 

【问题讨论】:

  • 你的意思是把json转成字符串? JSON.stringify()
  • 我在我的 ajax 中做到了。
  • 当我进入开发工具时,它返回的表单数据为:{"name":"John","email":"test@gmail.com","message":"this is parsed "}:
  • 您应该在 req.body 中获取所有值,无需访问 .data。
  • var mailOptions = { from: req.body.email, to: 'augustshah@02pilot.com', subject: 'Quote', text: req.body.message };当我提交表单时,它发送的电子邮件没有表单数据。

标签: javascript jquery node.js ajax express


【解决方案1】:

已修复:很简单。在我的 script.js 中,我没有设置 contentType。我将它设置为“应用程序/json”并解决了我的问题。

【讨论】:

    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多