Page 153 - AngularJS权威教程
P. 153

15.6 设置$httpProvider   133


                  调用模块的.factory()方法来创建拦截器,可以在服务中添加一种或多种拦截器:                                          1

                  angular.module('myApp', [])
                  .factory('myInterceptor', function($q) {
                      var interceptor = {                                                            2
                          'request': function(config) {
                              // 成功的请求方法
                              return config; // 或者 $q.when(config);                                  3
                          },
                          'response': function(response) {
                              // 响应成功
                              return response; // 或者 $q.when(config);                                4
                          },
                          'requestError': function(rejection) {
                              // 请求发生了错误,如果能从错误中恢复,可以返回一个新的请求或promise                                5
                              return response; // 或新的promise
                              // 或者,可以通过返回一个rejection来阻止下一步
                              // return $q.reject(rejection);                                        6
                          },
                          'responseError': function(rejection) {
                              // 请求发生了错误,如果能从错误中恢复,可以返回一个新的响应或promise
                              return rejection; // 或新的promise                                        7
                              // 或者,可以通过返回一个rejection来阻止下一步
                              // return $q.reject(rejection);
                          }                                                                          8
                      };
                      return interceptor;
                  });
                                                                                                     9
                  我们需要使用$httpProvider在.config()函数中注册拦截器:

                  angular.module('myApp', [])                                                        10
                  .config(function($httpProvider) {
                      $httpProvider.interceptors.push('myInterceptor');
                  });
                                                                                                     11
              15.6 设置$httpProvider
                                                                                                     12
                  使用.config()可以向所有请求中添加特定的HTTP头,这非常有用,尤其是我们希望将身份
              验证的头同请求一同发送,或设置响应类型的时候。
                                                                                                     13
                  默认的请求头保存在$httpProvider.defaults.headers.common对象中。默认的头如下所示:

                  Accept: application/json, text/plain, */*                                          14
                  通过.config()函数可以对这些头进行修改或扩充,如下所示:

                  angular.module('myApp', [])                                                        15
                  .config(function($httpProvider) {
                      $httpProvider.defaults.headers
                          .common['X-Requested-By'] = 'MyAngularApp';                                16
                  });
                  也可以在运行时通过$http对象的defaults属性对这些默认值进行修改。例如,通过如下方
              法可以动态添加一个头:                                                                            17

                  $http.defaults
                      .common['X-Auth'] = 'RandomString';、                                           18
   148   149   150   151   152   153   154   155   156   157   158