$ http不会在请求中发送Cookie

我们正在开发一个带AngularJS和Java Servlets的REST风格的Web服务。 当用户login时,我们的后端发送一个“Set-Cookie”头到前端。 在Angular中,我们通过$cookies (ngCookie – 模块)访问头文件并设置它。 在这里输入图像描述

现在,用户login,他可以例如删除一些东西。 所以前端发送GET请求到后端。 因为我们在不同的领域工作,所以我们需要设置一些CORS头文件,Angular在实际的GET请求之前做一个OPTIONS请求:

选项请求: 选项请求

GET请求 GET请求

我们通过$ http模块在Angular中执行此操作,但它不会发送包含JSESSIONID的cookie。

我怎样才能让Angular发送cookies?

在你的configuration中,DI $httpProvider然后设置withCredentials为true:

 .config(function ($httpProvider) { $httpProvider.defaults.withCredentials = true; //rest of route code }) 

关于angularjs withCredentials的信息: http ://docs.angularjs.org/api/ng.$http

哪些链接到mozilla文章: https : //developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS? redirectlocale = en-US & redirectslug = HTTP_access_control#section_5

 $http.get("URL", { withCredentials: true }) .success(function(data, status, headers, config) {}) .error(function(data, status, headers, config) {}); 

另外要记住的是:您需要启用第三方Cookie 。 如果您在Chrome中全局禁用了该function,请点击域名左侧的“我” – 符号,然后select“阻止”,然后取消屏蔽目标域名。