我想传入对 api 进行 REST 调用所需的用户名和密码组合。
控制器.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 原始标头放在请求的资源上,因为我无权编辑 API 的响应。