Pau*_*aul 7 http http-headers angularjs
我想$http
从一个特定请求中删除一些请求头字段(这意味着不在该$httpProvider
级别上).这些字段是:
如何在一个请求中执行此操作?我试图使用transformRequest
参数,但没有找到足够的信息使其工作.这样的[CoffeeScript]代码:
$scope.logout = ->
$http({
method: 'GET'
url: '/api/logout'
headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }
transformRequest: (data, headersGetter) ->
console.log data
console.log headersGetter
data
}).success ->
$location.path('editor')
Run Code Online (Sandbox Code Playgroud)
显示的data
是undefined
,headersGetter是function (c){a||(a=Nb(b));return c?a[y(c)]||null:a}
(它绝对没有告诉我),我不明白从transformRequest函数返回什么.
如果你使用Angular的非限制版本,当异常发生时你会得到更好的回溯,你可以更容易地反省角度代码.我个人在开发时推荐它.这是headersGetter
实际看起来像:
function (name) {
if (!headersObj) headersObj = parseHeaders(headers);
if (name) {
return headersObj[lowercase(name)] || null;
}
return headersObj;
}
Run Code Online (Sandbox Code Playgroud)
data
除非您正在发布一些数据,否则您的变换器的参数将是未定义的.
如果要获取单个标头,则该headersGetter
函数采用可选参数name
,但您省略了设置标头的参数:
headersGetter()['Cache-Control'] = 'no-cache';
headersGetter()['X-Requested-With'] = '';
Run Code Online (Sandbox Code Playgroud)
变换器的返回值应该是data
您要使用的值.