[英]How to add add request parameter to every Angular.js $http request (to start a xdebug session for example)
My hybrid application is based on AngularJS and uses a php REST api. 我的混合应用程序基于AngularJS并使用php REST api。
I would like to debug the php api directly from my Angular app instead to use REST console or Postman. 我想直接从我的Angular应用程序调试php api,而不是使用REST控制台或Postman。 It would save a lot of time especially for POST and PUT requests.
这将节省大量时间,特别是对于POST和PUT请求。
In order to do so I would need to add a parameter to each request like so: 为此,我需要为每个请求添加一个参数,如下所示:
http://localhost:8000/api/contacts?XDEBUG_SESSION_START=PHPSTORM
Can I config $http to do so? 我可以配置$ http这样做吗?
You can use httpInterceptor for that (official $http documentation contains more info) 您可以使用httpInterceptor(官方$ http 文档包含更多信息)
// register the interceptor as a service
$provide.factory('xdebugInterceptor', function($q) {
return {
// optional method
'request': function(config) {
// do something on success
// !!! adjust the config object
// add request param XDEBUG_SESSION_START=PHPSTORM
// it will be added to every made request
config.params = config.params || {};
config.params.XDEBUG_SESSION_START: "PHPSTORM";
return config;
},
// optional method
'requestError': function(rejection) {
// do something on error
return $q.reject(rejection);
},
// optional method
'response': function(response) {
// do something on success
return response;
},
// optional method
'responseError': function(rejection) {
// do something on error
return $q.reject(rejection);
}
};
});
// make this conditional so you use it only in DEV mode
$httpProvider.interceptors.push('xdebugInterceptor');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.