我试图在离子应用中设置Mailgun mail service。这是代码: 控制器:
$http({
"method": "POST",
"url": "https://api.mailgun.net/v3/" + mailgunUrl + "/messages",
//"crossDomain": "true",
"headers": {
"Access-Control-Allow-Origin": "*",//"http://localhost:8100",
"Access-Control-Allow-Headers": "content-type, accept",
//"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
"Content-Type": "application/x-www-form-urlencoded",
'Authorization' : 'Basic '+ mailgunApiKey
//"Authorization": "Basic " + mailgunApiKey//$base64.encode('api:key-'+mailgunApiKey)
},
data: "from=" + "no-reply@ineevent.com" + "&to=" + $scope.datapopup.mail + "&subject=" + "Guestlist" + "&text="
config.xml中
<content src="main.html"/>
<access origin="*"/>
<plugin name="cordova-plugin-whitelist" version="1"/>
<plugin name="cordova-plugin-crop" spec="~0.1.0"/>
<allow-navigation href="*" />
<allow-intent href="*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-intent href="tel:*"/>
<allow-intent href="sms:*"/>
<allow-intent href="mailto:*"/>
<allow-intent href="geo:*"/>
我收到status '0' error
并且日志显示不允许使用CORS(跨域请求),并且缺少access-control-allow-headers(翻译自法语)。
Chrome的另一个错误是'Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.'
。
我也尝试过Android设备但是没有用。有什么想法吗?
答案 0 :(得分:1)
将此添加到index.html
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
并将cordova-plugin-whitelist插件添加到您的应用中。
出于开发目的,您可以将CORS plugin用于Chrome。
您的控制器可以是这样的:
.controller("EmailCtrl", function($scope, $http) {
var mailgunUrl = "YOUR_DOMAIN_HERE";
var mailgunApiKey = window.btoa("api:key-YOUR_API_KEY_HERE")
$scope.recipient = "mail@gmail.com";
$scope.send = function(recipient, subject, message) {
$http(
{
"method": "POST",
"url": "https://api.mailgun.net/v3/" + mailgunUrl + "/messages",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + mailgunApiKey
},
data: "from=" + "ionic@example.com" + "&to=" + recipient + "&subject=" + subject + "&text=" + message
}
).then(function(success) {
console.log("SUCCESS " + JSON.stringify(success));
}, function(error) {
console.log("ERROR " + JSON.stringify(error));
});
}
})
答案 1 :(得分:0)
答案很简单:
当你从离子运行android时,你不应该使用任何可选参数: 离子运行android