我正在制作一个使用Mailgun API发送电子邮件的AJAX调用。 Mailgun上的文档说,发布请求应该是“https://api.mailgun.net/v3/domain.com/messages”。我已经包含了mailgun指定的api密钥(他们指示使用'api'的用户名)。由于这涉及CORS,我无法通过错误:请求头字段Access-Control-Allow-Headers不允许授权。
但是,我检查了网络选项卡中的请求/响应,并且来自Mailgun的响应中的“Access-Control-Allow-Origin”被设置为“*”...这应该表明它应该允许它? (请参阅下面的请求/响应):我已经编辑了实际的域和我的API密钥。
Remote Address:104.130.177.23:443
Request URL:https://api.mailgun.net/v3/domain.com/messages
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:POST
Connection:keep-alive
Host:api.mailgun.net
Origin:null
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Response Headersview source
Access-Control-Allow-Headers:Content-Type, x-requested-with
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:600
Allow:POST, OPTIONS
Connection:keep-alive
Content-Length:0
Content-Type:text/html; charset=utf-8
Date:Fri, 20 Mar 2015 19:47:29 GMT
Server:nginx/1.7.9
我的ajax调用代码如下所示,其中我将我的凭据包含在标题中以及应该发布帖子的域中。不知道是什么导致这不起作用。是因为我正在本地主机上测试吗?我认为,由于响应头中的“访问控制允许原点:*”,这会产生重大影响。任何帮助将不胜感激!谢谢。
function initiateConfirmationEmail(formObj){
var mailgunURL;
mailgunURL = "https://api.mailgun.net/v3/domain.com/messages"
var auth = btoa('api:MYAPIKEYHERE');
$.ajax({
type : 'POST',
cache : false,
headers: {"Authorization": "Basic " + auth},
url : mailgunURL,
data : {"from": "emailhere", "to": "recipient", etc},
success : function(data) {
somefunctionhere();
},
error : function(data) {
console.log('Silent failure.');
}
});
return false;
}
答案 0 :(得分:-1)
Drazisil在上面是正确的。响应需要包含var hapi = require('hapi');
var server = new hapi.Server();
var intert = require('inert');
var routes = require('./routes');
server.connection({port: 3000});
server.register(intert, function () {
});
server.route(routes);
server.route({
method: 'GET',
path: '/{path*}',
handler: {
directory: {
path: 'src/client/main/',
listing: false,
index: true
}
}
});
server.route({
method: 'GET',
path: '/app/{param*}',
handler: {
directory: {
path: 'src/client/main/app/',
redirectToSlash: true,
index: true
}
}
});
server.route({
method: 'GET',
path: '/bower_components/{params*}',
handler: {
directory: {
path: 'bower_components/'
}
}
});
server.start(function () {
console.log('Running server at ', server.info.uri);
});
,因为您在请求中包含该标头,并且Access-Control-Allow-Headers: Authorization
不是simple header。