Ionic as an application, certainly and the server has data exchange, decentralized processing API is too cumbersome, so the general use of interceptors to focus on processing.
Mainly by the following aspects of the application:
- Server Address Injection
- Error Handling
- Token injection
- Log Processing
- Skip to login page when no token
- ...
There are four ways of
Request:function (config) requesterror:function (config) response:function (response) Responseerror:function ( Response
Basically look at the method name to know its meaning. For specific usage and wording, please refer to: http://my.oschina.net/ilivebox/blog/290881?p=1
1, server injection, token injection:
Request:function (config) { if (config.params) config.params[' x-session-token '] = Cacheservice.token (); Remote service, unified Add server address var uri = Config.url; if (Config.method = = "JSONP") { Config.url = fgethost () + URI; Config.uri = URI; } return config;}
2. Token timeout:
Response:function (response) { if (Response.config.method = = "JSONP") { if (!fiswhiteuri ( Response.config.uri) { var login = response.data[' X-session-login ']; Force the user to log on if (!login) { $location. Path ('/user/login '); Return $q. Reject (response); }} return response;}
Description: 1, each time the incomingX-session-token parameters for back-end validation;
2. Back in the background
X-session-login:false
Indicates that token expires and needs to be logged back in.
Ionic's Angular Interceptor