![](/img/trans.png)
[英]How to add new headers to the http request in angularjs through interceptors?
[英]How to send the http request through interceptors in angularjs?
通常我们发送http请求为
var req={
}
and add req to the $http.get(req).....
但是如何从拦截器发送请求?
您想要创建将构建或修改您的请求的拦截器。
$ httpProvider提供程序包含一系列拦截器。 拦截器只是注册到该阵列的常规服务工厂。 这是我们创建拦截器的方式:
创建一个拦截器作为工厂。 拦截器是Angular中的工厂方法,如下所示。
var app = angular.module('app', []);
拦截器来了
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
}
};
});
在$ httpProvider中解决其依赖性。 在主模块文件中包含此行。 可能是app.js或其他
$httpProvider.interceptors.push( 'myHttpInterceptor');
现在,您可以通过拦截器发送$ http请求。 希望这对您有所帮助。 您可以在http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/中了解拦截器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.