如何使用angularjs

时间:2016-05-10 19:55:14

标签: javascript angularjs rest email mailgun

我使用以下代码使用mailgun API

在angularjs中发送电子邮件
.controller("MailgunController", function($scope, $http) {

  var mailgunUrl = "YOUR_DOMAIN_HERE";
  var mailgunApiKey = window.btoa("api:key-YOUR_API_KEY_HERE")

  $scope.send = function() {
    $http({
      "method": "POST",
      "url": "https://api.mailgun.net/v3/" + mailgunUrl + "/messages",
      "headers": {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": "Basic " + mailgunApiKey
      },
      data: "from=" + "test@example.com" + "&to=" + "soeone@gmail.com" + "&subject=" + "MailgunTest" + "&text=" + "EmailBody"
    }).then(function(success) {
      console.log("SUCCESS " + JSON.stringify(success));
    }, function(error) {
      console.log("ERROR " + JSON.stringify(error));
    });
  }

})

但是我收到以下错误!

XMLHttpRequest无法加载https://api.mailgun.net/v3/MY-URL/messages。请求标头字段预检响应中的Access-Control-Allow-Headers不允许授权。

我在代码中更改了以下内容

  1. YOUR_DOMAIN_HERE with-my-domain
  2. key-YOUR_API_KEY_HERE with with-my-api-key

1 个答案:

答案 0 :(得分:0)

您需要将以下内容添加到index.html

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

此外,您可以将CORS extension添加到Google Chrome。