javascript - 访问 $http 中的 'Set-Cookies' header

标签 javascript angularjs http-headers session-cookies

我是 AngularJS 的新手,堆放在简单的地方。

我运行简单的代码:

$http.post('/api/login/', {"user":"alexey","password":"alexey"})
     .success(function(data, status, headers, config){
         console.log(headers());
     } );

结果在控制台中:

Object {content-type: "application/json"}

但实际 header 包含许多其他值(来自 Chrome 网络检查器):

HTTP/1.0 202 ACCEPTED
Date: Sun, 12 Oct 2014 17:39:33 GMT 
Server: WSGIServer/0.1 Python/2.7.5
Vary: Accept, Cookie 
X-Frame-Options: SAMEORIGIN
Content-Type: application/json 
Allow: POST, OPTIONS 
Set-Cookie: csrftoken=rNzbxI3QXBoyBS9XaMCdVHmZ32k8VN3c; expires=Sun, 11-Oct-2015 17:39:33 GMT; Max-Age=31449600; Path=/ 
Set-Cookie: sessionid=jxwsm9b5wuelpnr3a00tuwonq0i9sksk; expires=Sun, 26-Oct-2014 17:39:33 GMT; httponly; Max-Age=1209600; Path=/ 

那么,我怎样才能读取另一个 header ,准确地从响应中读取 cookie?

最佳答案

出于安全原因,您无法访问来自 XMLHttpRequest 的响应中的 Set-Cookie header 。 AngularJS $http 使用浏览器的 XMLHttpRequest 实现。

4.7.4 The getAllResponseHeaders() method

Returns all headers from the response, with the exception of those whose field name is Set-Cookie or Set-Cookie2.

关于javascript - 访问 $http 中的 'Set-Cookies' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26329825/

相关文章:

AngularJS : What is the difference between $eval() and $parse in AngularJS?

angularjs - 使用 angularjs UI-Router 的文件下载链接

http - HTTP 响应结束

http-headers - Internet Explorer 9 不忽略缓存 header

javascript - React Router v4 全局不匹配嵌套路由子项

c# - 在 session 过期之前提醒用户,可以选择续订 session

javascript - d3.js svg 元素上的 Angular-UI 工具提示

javascript - GUI 线程无法使用 jQuery 正确更新

javascript - 为什么我收不到邮件?

c# - 如何使用 System.Net.Http 发送如下所示的 cURL 请求?