Angular远程服务器web服务授权
Angular remote server web service authorization
我试图从远程web服务与我的角应用程序获取数据。由于服务器使用WSO2 api管理器来处理请求,我需要通过"授权:承载31363a37a017a4b2e9b1104981ff"连同请求。
My request looks follows
movieControllers.controller('MovieListCtrl', ['$scope', '$http',
function($scope, $http) {
$http.jsonp('http:myserverurl/1.0.0'
,{
headers: {Authorization: 'Bearer AUTH_CODE_HERE'}
}).success(function(data, status, headers, config) {
$scope.movies= data;
$scope.status = status;
}).error(function(error, status, headers, config) {
$scope.status = status;
});
}]);
但是它给了我"未经授权的访问"。似乎标题没有正确设置。然后我直接请求我的api。它不需要任何授权。它工作得很好。
我对angular和API和web服务都是新手。我需要为Oauth安装额外的模块吗?(我想这个授权是真的。如果我说错了请指正)。谁能给我指出正确的方向,非常感谢。谢谢。
如果分配了Authorization,则在$http.jsonp
之前。它会工作的
$http.defaults.headers.common. Authorization = 'Bearer AUTH_CODE_HERE'
在这种情况下你的代码应该是-
movieControllers.controller('MovieListCtrl', ['$scope', '$http',
function($scope, $http) {
$http.defaults.headers.common. Authorization = 'Bearer AUTH_CODE_HERE';
$http.jsonp('http:myserverurl/1.0.0')
.success(function(data, status, headers, config) {
$scope.movies= data;
$scope.status = status;
}).error(function(error, status, headers, config) {
$scope.status = status;
});
}]);
我还建议创建拦截器,因为拦截器在这里充当请求生成器。
你所要做的就是在$httpProvider
中提到拦截器 var myApp = angular.module('myApp');
//Set configs
myApp.config(function($httpProvider){
//Add dependencyof interceptor
$httpProvider.interceptors.push( 'myInterceptor');
});
现在在工厂中创建一个拦截器
//Create you interceptor here
mayApp.factory('myInterceptor', function() {
return {
request: function(config) {
config.headers[ 'Authorization' ] = 'Bearer AUTH_CODE_HERE';
return config;
}
}
});
这样,您就解决了在每个请求中添加授权头的问题。如果你想要更改配置或想要为每个请求动态生成授权头,这个拦截器也会很有用。希望这对你有所帮助。干杯!
相关文章:
- 如何使用WCF服务和javascript表单post上传.doc文件
- 监视函数从服务返回不起作用,但作用域函数起作用
- 通过Magento的网络服务检索运费
- 在同一个服务工作者中处理service-worker.js有任何影响吗
- Flash Uploadify在调用我的MVC控制器时没有保留会话/授权
- 使用angular.js问题的JavaSpringREST服务
- AngularJS-需要在index.html页面中访问来自服务的数据
- 如何在Ionic2测试版中包含Ionic.io服务
- 基于api密钥的NodeJS web服务
- Redux应用程序结构-在哪里放置服务/业务逻辑
- Liferay:从portlet调用JSON服务
- 使用javascript在客户端上使用Web服务
- 如何通过安全的https连接在javascript中使用基于soap xml的Web服务
- 从Javascript连接到SOAP Web服务
- 如何检测服务广告维度
- 很少有角服务,只有常数不同
- 有没有办法使用浏览器从 RESTful 网络服务下载文件,该文件要求授权标头
- 授权我的应用程序使用我的web服务
- 401未经授权的错误读取Azure移动服务表作为认证的用户(Javascript)
- Angular远程服务器web服务授权