2016-04-06 49 views
0

我正在使用角度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. 是放验证的base64头内正确的方式?
  2. 这是服务器的问题还是我做错了什么?
  3. 是否有任何其他方式来测试凭据?
  4. 有没有其他的服务器可以测试Auth?我发现https://httpbin.org/但它只支持HTTP GET

回答

1

您确实已经正确设置了授权标头。然而,postservertest.com已禁止该头经由Access-Control-Allow-Headers使用:

$ 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

+0

你的意思是http://requestb.in对不对?如果是,请编辑。我试着编辑你的,但改变需要至少6个字符 –

+0

你能告诉我这是什么输出? –

+0

啊,对不起。它确实是requestb.in。该输出来自一个名为“HEAD”的小工具,它发出针对给定URL的“HEAD”请求。 'curl -I'应该达到同样的效果。 – DaSourcerer

相关问题