在 AngularJS 中的 $http ajax 调用中处理响应 headers

Processing Response headers in a $http ajax call in AngularJS

我正在尝试处理响应 header,但不确定如何查询它们。这是代码片段。当我试图阅读 header 时,我注释掉了几行并对我注意到的内容发表了一些评论。

$http({
method: 'POST',
url: URL,
data: $.param(data),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function (data,status,headers) {
//Need to process the header to understand the server response
//console.log(headers()); //This returns null
//console.log(headers('custom-myapp-text');// Obvisouls returns null as the headers() returns null
deferred.resolve();
});
return deferred.promise;
};

根据他们的 documentation,'headers' returns 一个函数??不确定如何基于此查询 header 值。

data – {string|Object} – The response body transformed with the transform functions.

status – {number} – HTTP status code of the response.
**headers – {function([headerName])} – Header getter function.**
config – {Object} – The configuration object that was used to generate the request.
statusText – {string} – HTTP status text of the response.

我刚用有效的 header 试了一下,没问题:

console.log(headers('Content-Length'));

此控制台在我的示例中记录了 2。它将允许您访问任何有效响应 headers.