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';