我使用mailgun api从我的测试应用程序发送邮件。每当我尝试发送邮件时,它都会给我:
Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.
有趣的是,当我在chrome中启用CORS插件时,它工作正常,并发送邮件。怎么了?我认为CORS必须在服务器端设置?那么为什么chrome会阻止我的请求?
使用代码:
var requestHeaders = new Headers();
requestHeaders.append('Authorization', 'Basic ' + this.mailgunApiKey);
requestHeaders.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.request(new Request({
method: RequestMethod.Post,
url: 'https://api.mailgun.net/v3/' + this.mailgunUrl + '/messages',
body: 'from=' + this.sourcePageMail + '&to=' + recipient + '&subject=' + subject + '&text=' + message,
headers: requestHeaders
}))
.subscribe(success => {
console.log('SUCCESS -> ' + JSON.stringify(success));
}, error => {
console.log('ERROR -> ' + JSON.stringify(error));
});
答案 0 :(得分:0)
您必须添加请求标题
Allow-Control-Allow-Origin: *
和OPTIONS
OPTIONS /resources/post-here/ HTTP/1.1
Host: bar.other