Я использую перехватчик $ http, чтобы настроить мои заголовки http.
app.factory('httpRequestInterceptor', ['$q', '$location', 'localStorageService', function ($q, $location, localStorageService) {
var _request = function (config) {
config.headers = config.header || {};
var authData = localStorageService.get('authData');
if (authData) {
config.headers.Authorization = 'Bearer ' + authData.token;
}
return config;
}
var _responseError = function (rejection) {
if (rejection.status == 401) {
$location.path('/login');
}
return $q.reject(rejection);
}
return {
request: _request,
responseError: _responseError
}
}]);
app.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
}]);
Без использования перехватчика $ http (это означает, что если я комментирую
$httpProvider.interceptors.push('httpRequestInterceptor')
), мой метод метода слов прекрасен.
$http.post(RoutePrefix + "api/accounts/create", dataToSend, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function (data, status, headers, config) {
console.log("succes!");
}).error(function (data, status, headers, config) {
console.log("Error!");
});
Но если я использую перехватчик $ http, я получу 415 (ошибка неподдерживаемых носителей).
message: "The request entity media type 'text/plain' is not supported for this resource."
exceptionMessage: "No MediaTypeFormatter is available to read an object of type 'CreateUserViewModel' from content with media type 'text/plain'."
exceptionType: "System.Net.Http.UnsupportedMediaTypeException"
Любое решение или предложение о моей проблеме? Заранее спасибо.
На линии:
config.headers = config.header || {}
В правой части отсутствует окончание s
символа после header
слова. Поэтому config.headers
инициализируется на {}
потому что config.header
не undefined
.
Но, даже если он был написан config.headers = config.headers || {}
config.headers = config.headers || {}
, Я думаю, что это вообще не нужно и не имеет никакого смысла.
config.header
вconfig.headers = config.header || {}
? Кажется, что он удаляет все существующие заголовки по запросу.