访问angularJS中的$ httpProvider.defaults.headers配置对象时出错

时间:2014-06-13 00:49:11

标签: angularjs

我正在尝试覆盖http标头中POST请求的标头默认值。即使我在函数参数中提供$ httpProvider,但它仍然是抛出错误。

代码是 -

 angular.module('myApp.services', ['ngCookies'])
.service('sharedProperties', function ($cookieStore, $http, $httpProvider) {

    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-                              form-urlencoded;charset=utf-8';

 });

错误是 -

错误:[$ injector:unpr]未知提供商:$ httpProviderProvider< - $ httpProvider< - sharedProperties

1 个答案:

答案 0 :(得分:11)

$ httpProvider只能在配置中访问...您应该更改代码,如:

angular.module('myApp.services', ['ngCookies'])
.config(function($httpProvider){
   $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
})
.service('sharedProperties', function ($cookieStore, $http) {

});