gpt4 book ai didi

javascript - AngularJS $http ".then"回调未使用 ExpressJS 和 sendgrid 触发

转载 作者:搜寻专家 更新时间:2023-11-01 00:29:55 24 4
gpt4 key购买 nike

我 Angular 这个功能

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 不执行了。

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

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.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});
});

关于javascript - AngularJS $http ".then"回调未使用 ExpressJS 和 sendgrid 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40424103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com