[英]How to send the http request through interceptors in angularjs?
Usually we send http request as 通常我们发送http请求为
var req={
}
and add req to the $http.get(req).....
But how to sent req from the interceptors ? 但是如何从拦截器发送请求?
You want to create interceptor which will build or modify your request. 您想要创建将构建或修改您的请求的拦截器。
The $httpProvider provider contains an array of interceptors.
$ httpProvider提供程序包含一系列拦截器。 An interceptor is simply a regular service factory that is registered to that array.
拦截器只是注册到该阵列的常规服务工厂。 This is how we create an interceptor:
这是我们创建拦截器的方式:
Create one interceptor as factory. 创建一个拦截器作为工厂。 Interceptors are the factory methods in Angular it will look like below.
拦截器是Angular中的工厂方法,如下所示。
var app = angular.module('app', []);
Here goes your interceptor 拦截器来了
app.factory('myHttpInterceptor', function() {
return {
//Config contains all your request details
request: function(config) {
//Do you stuff here. Modify headers,pass params etc
return config;
};
},
response: function(response) {
//Do you stuff here with response if any and return
return response;
},
responseError: function(responseError) {
//Handle response error here
}
};
});
Resolve its dependency in $httpProvider. 在$ httpProvider中解决其依赖性。 Include this line in you main module file.
在主模块文件中包含此行。 it may be app.js or nything
可能是app.js或其他
$httpProvider.interceptors.push( 'myHttpInterceptor');
Now by this way you can send $http request via interceptors. 现在,您可以通过拦截器发送$ http请求。 Hope this helps to you.
希望这对您有所帮助。 You can read about interceptors here http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/
您可以在http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/中了解拦截器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.