$http 不在请求中发送 cookie [英] $http doesn't send cookie in Requests
问题描述
我们正在使用 AngularJS 和 Java Servlet 开发 RESTful Web 服务.当用户登录时,我们的后端向前端发送一个Set-Cookie"标头.在 Angular 中,我们通过 $cookies
(ngCookie - 模块)访问标头并设置它.
We are working on a RESTful Webservice with AngularJS and Java Servlets.
When the user logs in, our backend sends a "Set-Cookie" header to the frontend. In Angular we access the header via $cookies
(ngCookie - module) and set it.
现在用户已经登录,他可以删除一些东西.因此,前端向后端发送 GET 请求.因为我们在不同的域上工作,所以我们需要设置一些 CORS 标头,而 Angular 在实际 GET 请求之前执行 OPTIONS 请求:
Now that the user is logged in he can for example delete some stuff. Therefore the frontend sends a GET request to the backend. Because we work on different domains we need to set some CORS Headers and Angular does an OPTIONS request before the actual GET request:
OPTIONS 请求:
GET 请求
我们通过 $http 模块在 Angular 中执行此操作,但它不会发送包含 JSESSIONID
的 cookie.
We do this in Angular via $http module, but it just won't send the cookie, containing JSESSIONID
.
如何启用 Angular 发送 cookie?
推荐答案
在您的配置中,DI $httpProvider
然后将 withCredentials 设置为 true:
In your config, DI $httpProvider
and then set withCredentials to true:
.config(function ($httpProvider) {
$httpProvider.defaults.withCredentials = true;
//rest of route code
})
关于 angularjs withCredentials 的信息:http://docs.angularjs.org/api/ng.$http
Info on angularjs withCredentials: http://docs.angularjs.org/api/ng.$http
Which links to the mozilla article: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5
这篇关于$http 不在请求中发送 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!