【发布时间】: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