Reputation: 6206
I have many requests in the same structure as this one:
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 );
}
};
} );
But when I try to set the Content-Type header in a separate interceptor, like this:
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 );
}
};
} );
I get this error:
ReferenceError: invalid assignment left-hand side
config.headers.Content-Type = 'application/x-www-form-urlencoded';
I'm already using another interceptor for authorization, which works fine:
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 );
}
};
} );
So how can I put other header types in interceptors? I checked the AngularJS documentation but did not found an answer there.
Upvotes: 1
Views: 1978
Reputation: 4290
You can't use -
in variable names.
Try:
config.headers["Content-Type"] = 'application/x-www-form-urlencoded';
Upvotes: 3