2016-01-24 44 views
0

我在Heroku上有一个应用程序http://random-name.herokuapp.com,它发送带有Mandrill的电子邮件。但是,无论我是否在本地运行在localhost:5000或远程应用程序在Heroku上的,试图发送电子邮件时,我抛出了以下错误:Mandrill XMLHttpRequest错误 - 无效的访问控制 - 允许源标头

的XMLHttpRequest无法加载 https://mandrillapp.com/api/1.0/messages/send.json。对 预检请求的响应未通过访问控制检查:当 凭证标志为true时,通配符'*' 无法用于'Access-Control-Allow-Origin'标头中。 Origin'http://random-name.herokuapp.com'是 因此不允许访问。

有堆栈溢出关于此错误的大量的文档资料(见CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true),因此,我把我的快递头部以下:

app.use(function(req, res, next) { 
    res.header("Access-Control-Allow-Origin", "http://localhost:5000 http://random-name.herokuapp.com"); 
    res.header('Access-Control-Allow-Credentials', true); 
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); 
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 
    next(); 
}); 

然而,这似乎并没有解决问题。我在Mandrill的文档中找不到任何有用的东西。我猜测我错误地指定了标题,或者甚至没有被使用。有任何想法吗?

回答

0

Access-Control-Allow-Origin取单个值,而不是空格分隔列表。

您需要返回Origin请求标头中的客户端发送的任何内容(测试以确保首先对您是可接受的来源!)。

+0

所以它看起来像他们响应头通配符: '访问控制允许来源:* 内容编码:gzip 的Content-Type:text/html的; charset = utf-8' –

+0

你是什么意思“返回任何客户端发送”? –

+0

@sir_thursday - 您尝试使用通配符,并且错误消息清楚地告诉您,因为您设置了'withCredentials',因此不允许使用通配符。 – Quentin

相关问题