我想传入用于对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? –