我已经阅读了足够多的书,知道我迷失了这一点。其他线程上的解决方案似乎没有帮助。
我在pages .samedomain.com 上有一个页面,在我的 Node 站点app .samedomain.com 中调用 mandrill api。使用 ORM,我可以很好地编写表路由。在表格被写入并且页面收到确认后,它应该触发到电子邮件路由。在本地运行时,两者都可以正常工作。部署时,我得到...
XMLHttpRequest 无法加载http://apps.samedomain.com/.../.../mail/4847775376401843。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问来源“ http://pages.samedomain.com ”。响应具有 HTTP 状态代码 502。
在我的 app.js 中,我有...
var cors = require('cors');
app.use(cors());
在我的路线文件中,我有...
module.exports = function(appRouter) {
var mandrill = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill(process.env.MANDRILL_API_KEY);
appRouter.route('/.../mail/:first_list_id').post(function(req,res){
req.models.know_me_2016
.find({list_id:req.params.first_list_id})
.run(function(err, results){
if (err) {
res.send(err);
} else {
var template_content = [{
"recipient": <stuff> ,
"content": <stuff>
}];
var message = {
<mandrill message object stuff>
};
}
mandrill_client.messages.sendTemplate({
"template_name": <template-name>,
"template_content": template_content,
"message": message}, function(result) {
console.log(result);
//I tried adding header stuff but it didn't help, maybe in wrong place? I thought CORS library was going to take care of this part?
res.header("Access-Control-Allow-Origin", "http://interactives.dallasnews.com");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//It sends successfully when run local
res.send("Email sent successfully");
}, function(e) {
// Mandrill returns the error as an object with name and message keys
console.log('A mandrill error occurred: ' + e.name + ' - ' + e.message);
// A mandrill error occurred: Unknown_Subaccount - No subaccount exists with the id 'customer-123'
});
});
});
}
我的 Mandrill 密钥设置为接受所有 IP。
任何见解将不胜感激。