我是 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/