【问题标题】:Need to call AngularJS function on every ajax request需要在每个 ajax 请求上调用 AngularJS 函数
【发布时间】:2016-10-25 21:12:15
【问题描述】:

我需要在我的应用程序发出的每个 Ajax 请求上调用我的 AngularJS 函数之一,并且我想使用 AngularJS 方式(不是经典的 JS/JQuery 方式)

这是我的代码:

.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push(['$location', '$q',  function($location, $q) {
        return {
            'request': function(request) {
                // My angularJS Function goes here.
                return request;
            },
            'responseError': function(response) {
                return $q.reject(response);
            }
        };
    }]);
}])

但它无法正常工作。

没有错误 - 控制台上没有输出。非常感谢您的帮助。

【问题讨论】:

    标签: angularjs ajax angular-http-interceptors


    【解决方案1】:

    发现导致此问题的重复 $httpProvider.interceptors。已修复,现在可以正常使用了。

    【讨论】:

      【解决方案2】:

      试试这个

         .config(['$httpProvider', function($httpProvider) {
              $httpProvider.interceptors.push(['$location', '$q',  function($location, $q) {
                   var requestPromise = {
                        request: function(request) {
                             // My angularJS Function goes here.
                             return request;
                        },
                        responseError: function(response) {
                             return $q.reject(response);
                        }
                   };
                   return requestPromise;
              }]);
          }])
      

      【讨论】:

        猜你喜欢
        • 2013-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-31
        • 2014-09-14
        相关资源
        最近更新 更多