在什么情况下,你在AngularJS定义HTTP拦截? [英] In what context do you define an HTTP Interceptor in AngularJS?

查看:126
本文介绍了在什么情况下,你在AngularJS定义HTTP拦截?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图定义一个拦截器在AngularJS来处理错误。

我已经遵循了AngularJS文档的说明但可以'不像是会做出正确的拦截寄存器,我不知道我是否做了正确的上下文:

  VAR editorApp = angular.module('editorApp',['editorAppServices'])
  .RUN(函数($ HTTP){
    $ http.defaults.headers.common.Accept ='应用/ JSON
  }
);
// ----------------------//注册一个拦截器(什么也不做)
$ httpProvider.interceptors.push(函数($ Q){
  返回{
   请求:功能(配置){
       // 做一点事
    },    回应:功能(响应){
       // 做一点事
    }
  };
});

当我添加了code上面我在控制台中看到以下错误:

 未捕获的Ref​​erenceError:$ httpProvider没有定义

我在哪里可以做到这一点,使得$ httpProvider定义?

编辑我与$方法提供了同样的问题...

我有,当我尝试在文档中指定登记 $提供方法没有被定义的东西了同样的问题:

  $ provide.factory('myHttpInterceptor',函数($ Q,dependency1,dependency2){
    返回{
      //可选的方法
      请求:功能(配置){
        //做事情成功
        返回配置;
      }
    // ...
}

控制台显示此错误:

 未捕获的Ref​​erenceError:$提供没有定义


解决方案

要注册一个拦截器使用配置()您AngularJS喜欢这个应用程序的功能:

  editorApp.config([$ httpProvider功能($ httpProvider){
     $ httpProvider.interceptors.push(
         // ...函数注册为一个拦截器
     );
}]);

您必须声明,这个功能应该有 $ httpProvider 的一个实例注入 - 这就是的第一个字符串参数配置()一样。

I'm trying to define an interceptor to handle errors in AngularJS.

I've followed the instructions in the AngularJS docs but can't seem to make the interceptor register correctly and I wonder whether I'm doing it in the right context:

var editorApp = angular.module('editorApp', ['editorAppServices'])
  .run(function($http) {
    $http.defaults.headers.common.Accept = 'application/json'
  }
);
// ----------------------

// register an interceptor (that does nothing)
$httpProvider.interceptors.push(function($q) {
  return {
   'request': function(config) {
       // do something
    },

    'response': function(response) {
       // do something
    }
  };
});

When I add the code above I see the following error in the console:

Uncaught ReferenceError: $httpProvider is not defined 

Where do I do this such that $httpProvider is defined?

Edit I have the same problem with the $provide method...

I have the same issue with things not being defined when I try the $provide method of registration that is specified in the docs:

$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
    return {
      // optional method
      'request': function(config) {
        // do something on success
        return config;
      }
    //...
}

The console shows this error:

Uncaught ReferenceError: $provide is not defined

解决方案

To register an interceptor use the config() function of your AngularJS application like this:

editorApp.config(["$httpProvider", function ($httpProvider) {
     $httpProvider.interceptors.push(
         // ... function to register as an interceptor
     );
}]);

You have to declare that this function should have an instance of $httpProvider injected - this is what the first string parameter of config() does.

这篇关于在什么情况下,你在AngularJS定义HTTP拦截?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆