繁体   English   中英

后端接收时,HTTP Post数据对象未完成

[英]HTTP Post data object not complete when received by backend

我正在尝试使用AngularJS中的联系表单发送电子邮件,服务器端已在NodeJS中编程,通过smtp发送电子邮件,我正在使用nodemailer库,而AngularJS方面表单正在发送数据,但服务器端却没有收到此数据,仅向我显示一个名为IncomingMessage的对象,其中包含许多项目,但看不到我的电子邮件数据。

角边

angular.module('contactApp', [])
.factory('postEmailForm',['$http',function($http){

return {

 postEmail: function(emailData,callback){
    console.log(emailData);
   $http.post("/contact-form", emailData).success(callback);
     }
   }
}])

.controller('ContactController', function ($scope,postEmailForm) {

$scope.sendMail = function () {
console.log("Entro!");
var req = {
 headers: {
   'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-     Type, Accept'
 },
 data:
  {
    contactName : this.contactName,
    contactEmail : this.contactEmail,
    contactMsg : this.contactMsg
  }
}

  postEmailForm.postEmail(req, function (res) {
      console.log(req);
    if (res.type == false) {
      //do something
    }else{

      console.log("OK");
    }

  })

 };

});

服务器端

var express=require('express');
var nodemailer = require("nodemailer");
var app = express();
var serveStatic = require('serve-static');
var http = require('http');
app.use(serveStatic("."));
app.get('/',function(req,res){
res.sendfile('index.html');
 }); 

  var smtpTransport = nodemailer.createTransport("SMTP",{
   service: "Gmail",
  auth: {
  user: "sending email",
  pass: "sending pass"
  }
  });

 app.post('/contact-form',function(req,res){
 //Your NodeMailer logic comes here
 console.log(req.data);

 var mailOptions={
 from : req.data.contactEmail,
 to: recipient email
 subject : "Contact",
 text : req.data.contactMsg
 }
  console.log(mailOptions);
  smtpTransport.sendMail(mailOptions, function(error, response){
  if(error){
  console.log('Server error ' + error);
  res.end("error");
 }else{
 console.log("Message sent: " + response.message);
  res.end("sent");
  }
});

我忘了在服务器端添加body-parser来解析数据

var bodyParser = require('body-parser');
app.use(bodyParser.json());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM