【问题标题】:AngularJS - Unknown provider configuring $httpProviderAngularJS - 配置 $httpProvider 的未知提供程序
【发布时间】:2013-06-23 08:19:34
【问题描述】:

在以下代码示例中:

myApp.config(['$httpProvider', function($httpProvider, $cookieStore) {

    $httpProvider.defaults.withCredentials = true;

    $httpProvider.defaults.headers.get['Authorization'] = 'Basic '+ $cookieStore.get('myToken');

    return JSON.stringify(data);

}]);

我收到一个 angularjs 错误,例如“未知提供者 $cookieStore”。

'myApp' 有dependenciy 和'ngCookies' 和 angular-cookies.min.js 是 laoded,那么该代码有什么问题?

这是我在 .config 中这样做的事实吗?

【问题讨论】:

  • 你为什么不也向我们展示你的myApp初始化代码。

标签: http angularjs configuration provider


【解决方案1】:

因为只能在配置时传递提供程序,所以我终于完成了对我的 http 参数的覆盖,而不是使用请求转换器,而是通过创建服务作为工厂来执行请求。

这是服务的代码示例(未测试,仅供参考):

angular.module('myapp-http-request', []);
angular.module('myapp-http-request')
.factory('MyRequests', function($http, $cookieStore){

    return {
        request: function(method, url, data, okCallback, koCallback){
            $http({
                method: method,
                url: url,
                data: data
            }).success(okCallback).error(koCallback);
        },
        authentifiedRequest: function(method, url, data, okCallback, koCallback){
            $http({
                method: method,
                url: url,
                data: data,
                headers: {'Authorization': $cookieStore.get('token')}
            }).success(okCallback).error(koCallback);
        }
    }
});

以及使用示例(未测试,仅供参考):

angular.module('sharewebapp', ['myapp-http-request'])
.controller('MyController', ['MyRequests', function(MyRequests){
    MyRequests.authentifiedRequest('DELETE', '/logout', '', function(){alert('logged-out');}, function(){alert('error');})
}]);

【讨论】:

  • 这也有问题,因为工厂的标题是静态的。尝试登录,然后退出,然后再次登录,您会发现它仍在使用旧令牌。
【解决方案2】:

您可能需要添加 cookieStore

myApp.config(['$httpProvider', '$cookieStore', function($httpProvider, $cookieStore) 

【讨论】:

  • 已经过测试,错误消失了,但后来我得到一个“无法在未定义上做”,即 $cookieStore 未定义
  • 哦,那是真的。在配置部分你只能使用提供者,而在 .run 部分你只能使用实例。我猜 cookieStore 不是提供者
  • 该死的。使用 $http 请求时是否有另一种方法可以动态设置标头?
  • 我认为您可以转换请求和响应标头。不过我自己没试过。检查docs.angularjs.org/api/ng.$http中的“转换”
  • 是的,实际上这段代码已经被放入了一个转换器,配置在 $httpProvider 上。但最后因为一起发现的原因,我通过使用“publicRequest”和“privateRequest”方法创建服务“RequestService”来使用 $http 并自动将我的令牌参数添加到 $http 来解决这个问题。我将使用响应和代码示例更新我的问题。感谢您的帮助
【解决方案3】:

我遇到了同样的问题,所以我会发布我是如何解决它的。我基本上使用 $injector 模块来手动获取我需要的服务实例。请注意,这也适用于用户定义的服务。

 angular.module('app').
 config(config);

 config.$inject = ['$httpProvider'];

 function config($httpProvider) {
  //Inject using the $injector
  $httpProvider.interceptors.push(['$injector', function($injector){
  return {
    request: function(config) {

      //Get access by injecting an instance of the desired module/service
      let $cookieStore = $injector.get('$cookieStore');

      let token = $cookieStore.get('your-cookie-name');
      if (token) {
        config.headers['x-access-token'] = token;
      }
      return config;
    }
  }
}])
}

【讨论】:

    【解决方案4】:

    使用 Module.run() 似乎是设置始终需要的标头的一种更简洁的方法。在这里查看我的答案:AngularJS pass requestVerificationToken to a service

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2019-09-27
      • 2013-08-23
      相关资源
      最近更新 更多