Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Can';t将内容类型标头放入拦截器_Angularjs_Http - Fatal编程技术网

Angularjs Can';t将内容类型标头放入拦截器

Angularjs Can';t将内容类型标头放入拦截器,angularjs,http,Angularjs,Http,我有许多与此相同结构的请求: angular.module( 'factories' ) .factory( 'encodedFormInterceptor', function( $window, $q, $http ) { return { request: function( config ) { config.headers = config.headers || {}; config.headers.Content-Type =

我有许多与此相同结构的请求:

angular.module( 'factories' )
  .factory( 'encodedFormInterceptor', function( $window, $q, $http ) {
    return {
      request: function( config ) {
        config.headers = config.headers || {};
        config.headers.Content-Type = 'application/x-www-form-urlencoded';

        return config || $q.when( config );
      },
      response: function( response ) {
        if ( response.status === 401 ) {

        }
        return response || $q.when( response );
      }
    };
  } );
但当我尝试在单独的拦截器中设置内容类型头时,如下所示:

angular.module( 'factories' )
  .factory( 'encodedFormInterceptor', function( $window, $q, $http ) {
    return {
      request: function( config ) {
        config.headers = config.headers || {};
        config.headers.Content-Type = 'application/x-www-form-urlencoded';

        return config || $q.when( config );
      },
      response: function( response ) {
        if ( response.status === 401 ) {

        }
        return response || $q.when( response );
      }
    };
  } );
我得到这个错误:

ReferenceError:左侧的分配无效

config.headers.Content-Type='application/x-www-form-urlencoded'

我已经在使用另一个拦截器进行授权,这很好:

angular.module( 'factories' )
  .factory( 'AuthInterceptor', function( $window, $q ) {
    return {
      request: function( config ) {
        config.headers = config.headers || {};
        if ( $window.localStorage.getItem( 'eva-token' ) ) {
          config.headers.Authorization = 'Bearer ' + $window.localStorage.getItem( 'eva-token' );
        }
        return config || $q.when( config );
      },
      response: function( response ) {
        if ( response.status === 401 ) {

        }
        return response || $q.when( response );
      }
    };
  } );

那么,如何将其他头类型放入拦截器中呢?我查看了AngularJS文档,但没有找到答案。

您不能在变量名中使用
-
。 尝试:

连字符呢?“config.headers.Content Type”看起来语法错误。
config.headers["Content-Type"] = 'application/x-www-form-urlencoded';