【问题标题】:AngularJS $http ".then" callback is not firing with ExpressJS and sendgridAngularJS $http ".then" 回调没有被 ExpressJS 和 sendgrid 触发
【发布时间】:2016-11-04 13:38:19
【问题描述】:

我有这个角度的功能

controller: function ($scope, $http) {
  $scope.sendEMail = function (referal) {
    // console.log('referal: ', referal);

    $http({
      url: '/send-referal',
      method: "POST",
      data: referal
    }).then(function (response) {
        console.log('success', response);
      },
      function (response) { // optional
        console.log('error', response);
      });
  };
}

现在.then 没有在之后执行。

在快递的服务器端,我有这样的端点:

app.post('/send-referal', function (req, res) {
  console.log('data: ', req.body);

  var html = "my html template";

  var helper = require('sendgrid').mail;
  var from_email = new helper.Email(req.body.email);
  var to_email = new helper.Email('foo@bar.com');
  var subject = 'Subject for referal form';
  var content = new helper.Content('text/html', html);
  var mail = new helper.Mail(from_email, subject, to_email, content);

  var sg = require('sendgrid')('api key here');
  var request = sg.emptyRequest({
    method: 'POST',
    path: '/v3/mail/send',
    body: mail.toJSON(),
  });

  sg.API(request, function(error, response) {
    console.log(response.statusCode);
    console.log(response.body);
    console.log(response.headers);
  });
});

这是 sendgrid 为您提供的正常示例。该函数正常发送电子邮件,但在客户端我根本没有收到任何响应,因此我可以告诉用户电子邮件已发送。

如何获得$http 请求以触发.then 操作?

【问题讨论】:

  • 您实际上并没有发送响应,因此客户端永远不会收到它。您需要对 res 属性执行一些操作,以将响应发送回客户端

标签: javascript angularjs node.js express sendgrid


【解决方案1】:

您必须使用res.json 从服务器发回响应,如下所示,

app.post('/send-referal', function (req, res) {
  console.log('data: ', req.body);

  var html = "my html template";

  var helper = require('sendgrid').mail;
  var from_email = new helper.Email(req.body.email);
  var to_email = new helper.Email('foo@bar.com');
  var subject = 'Subject for referal form';
  var content = new helper.Content('text/html', html);
  var mail = new helper.Mail(from_email, subject, to_email, content);

  var sg = require('sendgrid')('api key here');
  var request = sg.emptyRequest({
    method: 'POST',
    path: '/v3/mail/send',
    body: mail.toJSON(),
  });

  sg.API(request, function(error, response) {
    console.log(response.statusCode);
    console.log(response.body);
    console.log(response.headers);
  });

  // Send the response back to client something like below
  res.json({success:true});
});

【讨论】:

    【解决方案2】:

    您的 $http 调用存在一些问题。修复它们后,您将了解有关调用的更多信息,并且如果后端正常(看起来不错),这将解决问题。

    您将 2 个函数(成功和错误处理函数)传递给 then。您应该将错误函数传递给catch。或者,您可以摆脱 then 和 'catch`,直接将这两个函数传递给 $http 调用。

    使用 then 和 catch 修复示例:

        $http({
          url: '/send-referal',
          method: "POST",
          data: referal
        }).then(function (response) {
            console.log('success', response);
        }).catch(function (response) { // optional
            console.log('error', response);
        });
    

    更新

    正如其他答案所指出的那样,您的回答悬而未决,即未成文。我建议你按照@Aruna 的回答来修复服务器端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-12
      • 2015-11-21
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多