te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - AngularJS Interceptor TypeError: Cannot read property 'headers' of undefined - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS Interceptor TypeError: Cannot read property 'headers' of undefined - Stack Overflow

programmeradmin3浏览0评论

I am Getting this Error for unknown reasons while trying to implement a AJAX Spinner loading code.

I don't understand where the header should be defined. I did console.log(config) but I can see headers: accept: text/html value there.

Below is my Code:

/**
* Spinner Service
*/

//Spinner Constants
diary.constant('START_REQUEST','START_REQUEST');
diary.constant('END_REQUEST','END_REQUEST');

//Register the interceptor service
diary.factory('ajaxInterceptor', ['$injector','START_REQUEST', 'END_REQUEST', function ($injector, START_REQUEST, END_REQUEST) {
    var $http,
    $rootScope,
    myAjaxInterceptor = {
        request: function (config) {
            $http = $http || $injector.get('$http');
            if ($http.pendingRequests.length < 1) {
                console.log(config);
                $rootScope = $rootScope || $injector.get('$rootScope');
                $rootScope.$broadcast(START_REQUEST);
            }
        }
    };

    return myAjaxInterceptor;
}]);

diary.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('ajaxInterceptor');
}]);

I am Getting this Error for unknown reasons while trying to implement a AJAX Spinner loading code.

I don't understand where the header should be defined. I did console.log(config) but I can see headers: accept: text/html value there.

Below is my Code:

/**
* Spinner Service
*/

//Spinner Constants
diary.constant('START_REQUEST','START_REQUEST');
diary.constant('END_REQUEST','END_REQUEST');

//Register the interceptor service
diary.factory('ajaxInterceptor', ['$injector','START_REQUEST', 'END_REQUEST', function ($injector, START_REQUEST, END_REQUEST) {
    var $http,
    $rootScope,
    myAjaxInterceptor = {
        request: function (config) {
            $http = $http || $injector.get('$http');
            if ($http.pendingRequests.length < 1) {
                console.log(config);
                $rootScope = $rootScope || $injector.get('$rootScope');
                $rootScope.$broadcast(START_REQUEST);
            }
        }
    };

    return myAjaxInterceptor;
}]);

diary.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('ajaxInterceptor');
}]);
Share Improve this question edited Aug 17, 2015 at 17:23 C1pher 1,9726 gold badges34 silver badges52 bronze badges asked Mar 29, 2015 at 9:51 ChanXChanX 3723 gold badges10 silver badges26 bronze badges 2
  • on what line you get this error? – Grundy Commented Mar 29, 2015 at 10:02
  • No Line Number is show by angular::: ====== TypeError: Cannot read property 'headers' of undefined at $get.serverRequest (angular.js:9366) at processQueue (angular.js:13248) at angular.js:13264 at Scope.$get.Scope.$eval (angular.js:14466) at Scope.$get.Scope.$digest (angular.js:14282) at Scope.$get.Scope.$apply (angular.js:14571) at bootstrapApply (angular.js:1455) at Object.invoke (angular.js:4203) at doBootstrap (angular.js:1453) at bootstrap (angular.js:1473) – ChanX Commented Mar 29, 2015 at 10:07
Add a ment  | 

2 Answers 2

Reset to default 12

I think I have the solution.

I've had the same problem under an AngularJS project where an interceptor is exactly defined the same as yours (https://docs.angularjs/api/ng/service/$http#interceptors)

To shorten, an interceptor catch the config and have to return it. And you forgot to.

So that would be:

request: function (config) {
    $http = $http || $injector.get('$http');
    if ($http.pendingRequests.length < 1) {
        $rootScope = $rootScope || $injector.get('$rootScope');
        $rootScope.$broadcast(START_REQUEST);
    }
    return config;
}

Here you have a full sample about how to implement a spinner using interceptors (wrapping the $rootScope in a service for better code readibility).

http://lemoncode/2013/07/31/angularjs-found-great-solution-to-display-ajax-spinner-loading-widget/

As you pointed out, this is deprecated (I have to update the post), the current structure I'm using (simplified inner code). I think the best could be to start from a plunker, maybe it has nothing to do with the way tou are implementing (let me search for a seed plunkr)

myapp.factory('httpInterceptor', ['$q', '$injector',
    function ($q, $injector) {

    return {
      'request': function(config) {
         // request your $rootscope messaging should be here?
        return config;
      },

     'requestError': function(rejection) {
        // request error your $rootscope messagin should be here?
        return $q.reject(rejection);
      },


      'response': function(response) {
        // response your $rootscope messagin should be here?

        return response;
      },

     'responseError': function(rejection) {
        // response error your $rootscope messagin should be here?

        return $q.reject(rejection);

      }
    };
  }
]);
发布评论

评论列表(0)

  1. 暂无评论