我想在我的 $http.
请求中注入(inject) Content-Type
。我有以下拦截器,但我没有收到任何错误/输出,我做错了什么?
.factory('Interceptor', ['$injector', '$q', function ($injector, $q) {
return function () {
return {
request: function (config) {
var deferred = $q.defer();
config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
deferred.resolve(config);
return deferred.promise;
}
};
};
}])
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.responseInterceptors.push('Interceptor');
}]);
最佳答案
.factory('Interceptor', ['$injector', '$q', function ($injector, $q) {
return {
request: function (config) {
config.headers = config.headers || {};
config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
return config;
},
response: function(response) {
return response || $q.when(response);
}
};
}])
我希望我有更多时间来测试这一点,但请尝试一下。这是我之前使用的代码的变体,用于将“Bearer” token 添加到 header 。
关于javascript - Angular 拦截器给出空白输出,没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34660574/