在AngularJS的$ http ajax调用中处理响应头
问题描述:
我正在尝试处理响应头,但不知道如何查询它们。这是代码片段。当我试图阅读标题并对我注意到的内容发表评论时,我评论了几行。
$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'返回一个函数??不知道如何根据这个查询标题值。
data - {string | Object} - 使用 转换函数转换的响应正文。
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.
答
我只是一个有效的头试了一下,感觉不错:
console.log(headers('Content-Length'));
此控制台在我的例子登录2
。它将允许您访问任何有效的响应标题。
+0
不知道为什么它不起作用,但它现在工作。可能我试图检索的标题不存在。 – user6123723
可能的重复http://stackoverflow.com/questions/19454477/angularjs-accessing-http-headers –
不带任何参数调用'headers()'将返回一个可用的所有标题的对象。然后您可以通过键获取单独的标题。什么是状态码?也许你的请求正在返回一个错误? – SirTophamHatt
@KevinFriedheim没有。该解决方案不起作用。正如你在描述中看到的,headers()返回null。您放入的链接指向将header()添加到返回数组的解决方案。这似乎并不奏效。我的问题是为什么以及如何阅读关于此header- {函数([headerName])的Angular文档}} – user6123723