2016-04-06 45 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

相關問題