最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mailgun API: Request header field Authorization is not allowed by Access-Control-Allow-Headers - Stack Overflow

programmeradmin0浏览0评论

How do I set up my .htaccess on my AngularJS application to prevent the following error message:

Failed to load /messages: Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

Here is my .htaccess file:

<IfModule mod_headers.c>
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "Content-Type, Authorization"
Header add Access-Control-Allow-Methods "GET, POST"
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html
</IfModule>

I am getting the error message on my website here, whenever the user tries to submit the contact form.

This is my code for the process:

function send(apiUrl, from, to, subject, body, apiKey) {
  var url = apiUrl;
  var dataJSON = {
    from: from,
    to: to,
    subject: subject,
    text: body,
    multipart: true
  };

  var req = {
    method: 'POST',
    url: url,
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
      'Authorization': 'Basic ' + $base64.encode('api:'+apiKey)
    },
    transformRequest: function (obj) {
      var str = [];
      for (var p in obj) {
        str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
      }
      return str.join('&');
    },
    data: dataJSON
  };

  $http(req).then(function (data) {
    if(data.data) {
      if(data.data.message === 'Queued. Thank you.') {
        $window.alert('Sent. Thank you.');
      }
      else {
        $window.alert(data.data.message);
      }
    }
    else {
      $window.alert('An error has occured. Please try again.');
    }
  }, function (data) {
    if(data.data) {
      if(data.data.message === 'Queued. Thank you.') {
        $window.alert('Sent. Thank you.');
      }
      else {
        $window.alert(data.data.message);
      }        }
    else {
      $window.alert('An error has occured. Please try again.');
    }
  });
}

How do I set up my .htaccess on my AngularJS application to prevent the following error message:

Failed to load https://api.mailgun/v3/example./messages: Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

Here is my .htaccess file:

<IfModule mod_headers.c>
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "Content-Type, Authorization"
Header add Access-Control-Allow-Methods "GET, POST"
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html
</IfModule>

I am getting the error message on my website here, whenever the user tries to submit the contact form.

This is my code for the process:

function send(apiUrl, from, to, subject, body, apiKey) {
  var url = apiUrl;
  var dataJSON = {
    from: from,
    to: to,
    subject: subject,
    text: body,
    multipart: true
  };

  var req = {
    method: 'POST',
    url: url,
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
      'Authorization': 'Basic ' + $base64.encode('api:'+apiKey)
    },
    transformRequest: function (obj) {
      var str = [];
      for (var p in obj) {
        str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
      }
      return str.join('&');
    },
    data: dataJSON
  };

  $http(req).then(function (data) {
    if(data.data) {
      if(data.data.message === 'Queued. Thank you.') {
        $window.alert('Sent. Thank you.');
      }
      else {
        $window.alert(data.data.message);
      }
    }
    else {
      $window.alert('An error has occured. Please try again.');
    }
  }, function (data) {
    if(data.data) {
      if(data.data.message === 'Queued. Thank you.') {
        $window.alert('Sent. Thank you.');
      }
      else {
        $window.alert(data.data.message);
      }        }
    else {
      $window.alert('An error has occured. Please try again.');
    }
  });
}
Share Improve this question edited Apr 28, 2018 at 23:26 sideshowbarker 88.4k29 gold badges215 silver badges212 bronze badges asked Apr 28, 2018 at 12:42 methuselahmethuselah 13.2k52 gold badges176 silver badges332 bronze badges 4
  • Usually this is set on the server code, not on .htaccess, but threre is no information about the backend. – desoares Commented Apr 28, 2018 at 12:51
  • I am using a Godaddy Wordpress hosting package – methuselah Commented Apr 28, 2018 at 12:54
  • have you taken a look here? stackoverflow./questions/25727306/… – desoares Commented Apr 28, 2018 at 12:59
  • Usually an Apache / nginx config issue – Mason Stedman Commented Apr 28, 2018 at 13:17
Add a ment  | 

1 Answer 1

Reset to default 12

You can’t make authenticated requests to the Mailgun API from frontend JavaScript code running in a browser. The Mailgun API intentionally doesn’t support that, per their own documentation:

NOTE: If used in the browser, a proxy is required to municate with the Mailgun api due to cors limitations. Also, do not publish your private api key in frontend code.

Specifically, for requests from frontend JavaScript code running in browsers, the Mailgun API doesn’t allow the Authorization request header. You can verify that with curl or such:

$ curl -X OPTIONS -H "Origin: https://marquesslondon." \
       -i https://api.mailgun/v3/marquesslondon./messages

HTTP/1.1 200 OK
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

Notice the value of the Access-Control-Allow-Headers response header that endpoint returns doesn’t include Authorization. That means browsers will block your frontend JavaScript code from sending that API endpoint any request that includes the Authorization request header.

As far as your changes to the .htaccess file for the http://marquesslondon. site, those are unnecessary and irrelevant; it it doesn’t matter what CORS headers you return from that (your) site, because it’s just the site initiating the request — you’re not sending any requests to it cross-origin.

Instead what matters are the CORS headers returned by the site you are actually sending a request to cross-origin, which is https://api.mailgun. And as explained above, that site returns a CORS Access-Control-Allow-Headers response header which tells browsers not to allow requests that include the Authorization header — and that’s what results in you seeing the Request header field Authorization is not allowed error message cited in the question.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论