我想傳入用於對REST調用api所需的用戶名和密碼組合。REST的簡單身份驗證使用角度服務的請求攔截器獲取調用
Controller.js
var app = angular.module('myApp', []);
//The service to put the interceptor configuration on (not on all $http method calls)
app.factory('getCallsApi', ['$http', function($http) {
return {
get: function(callback) {
$http.get(/* REST URI */).success(function(data) {
callback(data);
});
}
}
}]);
var Interceptor = function($q) {
return {
request: function(config) {
//TODO: Pass in username and password (hardcoded) to get through authentication
},
requestError: function(rejection) {
},
response: function(config) {
//TODO: ADD Cross origin headers
},
responseError: function(rejection) {
}
}
}
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(Interceptor);
}]);
myApp.controller('appController', ['$scope','getCallsApi', function($scope, getCallsApi) {
getCallsApi.get(function(data) {
console.log(data);
});
}]);
我目前收到在控制檯的兩個錯誤。首先是未授權訪問的401狀態,以及有關在請求的資源上找不到交叉源標題的錯誤。我無法將X origin標頭放在請求的資源上,因爲我沒有權限編輯該API的響應。
如果要查詢對服務器不允許跨起源,和你沒有控制服務器來添加標題,那麼***控制服務器的人需要爲你做。沒有辦法繞過這個安全功能。 – Claies
是的,我知道這是一個很長的截獲反應,然後添加標題。但無論如何,如何通過服務調用的請求攔截器來通過401? –