【发布时间】:2015-04-05 15:14:32
【问题描述】:
我正在尝试使用 sendgrid 从应用程序发送电子邮件。这应该不会太难,而且我以前用 PHP 发送过电子邮件。在这种情况下,我想用 Javascript 来做,因为它是 Ember 应用程序的一部分。第一个问题是“No 'Access-Control-Allow-Origin”消息,我试图用 CORS 解决。现在我遇到了一个不同的错误!
现在我不知道该去哪里解决这个问题。我使用的代码如下:
(function(){
makeCorsRequest('GET', mailUrl);
})();
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
function makeCorsRequest(type, url) {
var xhr = createCORSRequest(type, url);
if (!xhr) {
alert('CORS not supported');
return;
}
xhr.onload = function() {
var text = xhr.responseText;
console.log(text);
var title = getTitle(text);
alert('Response from CORS request to ' + url + ': ' + title);
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
这给了我错误:
The 'Access-Control-Allow-Origin' header has a value 'https://sendgrid.com' that is not equal to the supplied origin. Origin 'http://localhost' is therefore not allowed access.
【问题讨论】:
-
您是从
https://sendgrid.com调用此代码吗?如果没有,您可以直接从 javascript 做任何事情。服务器必须发送到您要使用的实际域或 *.
标签: javascript xmlhttprequest cors sendgrid