如何拦截$资源请求 [英] How to intercept $resource requests
问题描述
会不会有拦截在$资源调用请求的方法吗?
我要一个OAUTHv2头添加的,而不是指定这对每个资源模型吧。
目前我只能够拦截的响应的,如在文档中阐明的:
...
拦截器 - {对象=} - 拦截对象有两个可选
方法 - 响应和responseError。这两种反应和responseError
拦截器被调用与HTTP响应对象。见$ HTTP
拦截器。
块引用>我知道你可以推$ HTTP一个全球性的拦截器,但我并不想在外面API调用的任何要求我不记名令牌(安全...)
如果谁在做OAUTHv2一定是遇到了这个问题。可惜没有在Angular.JS ...
没有标准的方法解决方案虽然,这不是明显的,还有一个办法拦截$资源请求。
下面是一个例子:
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<间的charset =UTF-8/>
<标题>拦截资源请求< /标题>
<风格类型=文/ CSS> .ng披风{显示:无; }< /风格>
&所述; SCRIPT SRC =angular.js>&下; /脚本>
&所述; SCRIPT SRC =角resource.js>&下; /脚本>
<脚本>
angular.module(应用程序,[ngResource])。
工厂(
服务,
[$资源功能($资源)
{
返回$资源(
http://md5.jsontest.com/
{},
{
MD5:
{
方法:GET,
PARAMS:{文字:空},
则:函数(解析)
{
this.params.text =***+ this.params.text +***;
this.then = NULL;
解决(本);
}
}
});
}])。
控制器(
测试,
[服务功能(服务)
{
THIS.VALUE =示例文本; this.call =功能()
{
this.result = services.MD5({文字:THIS.VALUE});
}
}]);
< / SCRIPT>
< /头>
<机身NG-应用=应用程序NG控制器=测试为测试>
<标签>文字:LT;输入类型=文本NG模型=test.value/>< /标签>
<输入类型=按钮值=呼NG点击=test.call()/>
< DIV NG绑定=test.result.md5>< / DIV>
< /身体GT;
< / HTML>如何工作的:
- $资源合并动作定义,请求参数和数据来建立一个$ HTTP请求的配置参数。
- 传递到$ HTTP请求在config参数被视为像对象的承诺,因此,它可能包含然后函数来初始化配置。
- 动作的则函数可以按照自己的意愿改造的要求。
该演示可以在变换请求被发现。 HTML
别处我已经展示用于取消$资源请求了类似的方法。
另请参阅:拦截angularjs资源请求
Would there be a way to intercept requests in the $resource call?
I want to add an OAUTHv2 header to it, instead of specifying this for every resource model.
Currently I'm only able to intercept the response, as stated in the docs:
...
interceptor - {Object=} - The interceptor object has two optional methods - response and responseError. Both response and responseError interceptors get called with http response object. See $http interceptors.
I know you can push a global interceptor on $http, but I don't want to include my Bearer token in any request outside API calls (security...)
Anybody who is doing OAUTHv2 must have come across this problem. A pity there is no standard way in Angular.JS...
解决方案Though, it's not obvious, there is a way to intercept $resource request.
Here is an example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Intercept resource request</title> <style type="text/css">.ng-cloak { display: none; }</style> <script src="angular.js"></script> <script src="angular-resource.js"></script> <script> angular.module("app", ["ngResource"]). factory( "services", ["$resource", function ($resource) { return $resource( "http://md5.jsontest.com/", {}, { MD5: { method: "GET", params: { text: null }, then: function(resolve) { this.params.text = "***" + this.params.text + "***"; this.then = null; resolve(this); } } }); }]). controller( "Test", ["services", function (services) { this.value = "Sample text"; this.call = function() { this.result = services.MD5({ text: this.value }); } }]); </script> </head> <body ng-app="app" ng-controller="Test as test"> <label>Text: <input type="text" ng-model="test.value" /></label> <input type="button" value="call" ng-click="test.call()"/> <div ng-bind="test.result.md5"></div> </body> </html>
How it works:
- $resource merges action definition, request params and data to build a config parameter for an $http request.
- A config parameter passed into an $http request is treated as a promise like object, so it may contain then function to initialize config.
- Action's then function may transform request as it wishes.
The demo can be found at transform-request.html
Elsewhere I've already shown a similar approach used to cancel $resource request.
See also: Intercept angularjs resource request
这篇关于如何拦截$资源请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!