[英]HTTP Basic Authentication in AngularJS
我正在嘗試在AngularJS
調用API。 API具有HTTP Basic Authentication
, username = admin
, password = admin123
這就是我調用API的方式,
$http.get('https://example.com', {
headers: {'Authorization': 'Basic YWRtaW46YWRtaW4xMjM='}
}).then(console.log("something"));
但是,它給了我401 UnAuthorized
響應。 推薦的傳遞用戶名和密碼的方法是什么? 我正在使用AngularJS 1
。
顯示錯誤,
OPTIONS https://example.com 401 ()
Failed to load https://example.com: Response for preflight has invalid HTTP status code 401.
Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","headers":{"Authorization":"Basic YWRtaW46YWRtaW4xMjM=","Accept":"application/json, text/plain, */*"},"url":"https://example.com"},"statusText":"","xhrStatus":"error"}
如果您的所有請求都需要授權標頭,則可能需要在更全局的級別上進行設置。 您可以通過添加
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common['Authorization'] = '<Basic Auth Header>';
}]);
在您的應用程序配置中。 要回答您的實際問題,我看不到您提供的代碼有什么特別的錯誤。 您能否提供一些有關服務器端期望以及它如何讀取授權的信息?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.