$http.post - 请求 header 字段 Access-Control-Allow-Headers 不允许授权

$http.post - Request header field Authorization is not allowed by Access-Control-Allow-Headers

我正在使用 angular js。 我有一个表单,要求用户输入 URL 基本身份验证详细信息,即 HTTP POST(如 PostMan)

的用户名和密码

我需要检查凭据是否有效。

我想出了以下代码:

$http({method: 'POST', url: $scope.serviceConfig.url, headers: {
    'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
})
    .success(function(res)
    {
        console.log(res);
    })
    .error(function(res)
    {
        console.log(res);
    });

我收到以下错误:

cannot load https://posttestserver.com/post.php. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

  1. 将 Auth base64 放入 headers 中是否正确?
  2. 这是服务器的问题还是我做错了什么?
  3. 还有其他方法可以测试凭据吗?
  4. 是否有任何其他服务器可以让我测试 Auth?我找到了 https://httpbin.org/ 但它只支持 HTTP GET

您确实以正确的方式设置了权限header。但是,postservertest.com 禁止通过 Access-Control-Allow-Headers:

使用 header
$ HEAD http://posttestserver.com
200 OK
Connection: close
Date: Wed, 06 Apr 2016 15:26:58 GMT
Server: Apache
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Origin: *

总之,您的代码是正确的。只是服务器不愿意接受你的请求。你可以试试 requestb.in.