在通过$.ajax()登录到一个网站后,我试图向该网站发送第二个$.ajax()请求-但当我检查使用FireBug发送的报头时,请求中没有会话cookie。

我做错了什么?


当前回答

我在跨域场景下操作。在登录期间,远程服务器返回set - cookie报头,并将Access-Control-Allow-Credentials设置为true。

下一次对远程服务器的ajax调用应该使用这个cookie。

CORS的Access-Control-Allow-Credentials允许跨域日志记录。查看https://developer.mozilla.org/En/HTTP_access_control获取示例。

对我来说,这似乎是JQuery的一个bug(或者至少是下一个版本的新特性)。

更新:

cookie不会从AJAX响应中自动设置(引用:http://aleembawany.com/2006/11/14/anatomy-of-a-well-designed-ajax-login-experience/) 为什么? 您无法从响应中获取cookie的值以手动设置(http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-getresponseheader) 我困惑. . 应该有一种方法可以让jquery.ajax()设置XMLHttpRequest。withCredentials = "true"参数。

答: 您应该使用http://api.jquery.com/jQuery.ajax/的xhrFields参数

文档中的示例如下:

$.ajax({
   url: a_cross_domain_url,
   xhrFields: {
      withCredentials: true
   }
});

服务器正确地响应这个请求也很重要。这里复制来自@Frédéric和@Pebbl的精彩评论:

重要提示:当响应一个已验证的请求时,服务器必须指定一个域,并且不能使用通配符。如果头部是通配符,上面的例子将失败:Access-Control-Allow-Origin: *

所以当请求是:

Origin: http://foo.example
Cookie: pageAccess=2

服务器应响应:

Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Credentials: true

[payload]

否则有效负载将不会返回到脚本。见:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Requests_with_credentials

其他回答

只是我的2美分设置PHPSESSID cookie问题时,在本地主机和开发环境下。我对本地主机上的REST API端点进行AJAX调用。说它的地址是mysite。Localhost /api/member/login/(我的开发环境中的虚拟主机)。

当我在Postman上执行此请求时,一切正常,PHPSESSID与响应一起设置。 当我通过AJAX从Browsersync代理页面请求这个端点时(例如,从122.133.1.110:3000/test/api/login.php在我的浏览器地址行,看到域是不同的vs myste .localhost) PHPSESSID不会出现在cookie中。 当我直接从同一域的页面(即mysite.localhost/test/api/login.php)发出这个请求时,PHPSESSID设置得很好。

所以这是一个跨源源请求cookie问题,正如上面@flu回答中提到的那样

如果您正在localhost或localhost上的端口(如localhost:8080)上进行开发,除了上面回答中描述的步骤外,还需要确保您没有在Set-Cookie报头中传递域值。 您不能在set - cookie报头中将域设置为localhost -这是不正确的-只需省略域即可。

查看cookie在本地主机与显式域和为什么asp.net不会在本地主机创建cookie ?

There are already a lot of good responses to this question, but I thought it may be helpful to clarify the case where you would expect the session cookie to be sent because the cookie domain matches, but it is not getting sent because the AJAX request is being made to a different subdomain. In this case, I have a cookie that is assigned to the *.mydomain.com domain, and I am wanting it to be included in an AJAX request to different.mydomain.com". By default, the cookie does not get sent. You do not need to disable HTTPONLY on the session cookie to resolve this issue. You only need to do what wombling suggested (https://stackoverflow.com/a/23660618/545223) and do the following.

1)在ajax请求中添加以下内容。

xhrFields: { withCredentials:true }

2)在不同子域的资源的响应头中添加以下内容。

Access-Control-Allow-Origin : http://original.mydomain.com
Access-Control-Allow-Credentials : true

使用

xhrFields: { withCredentials:true }

作为我的jQuery ajax调用的一部分只是解决方案的一部分。我还需要有从我的资源的选项响应返回的头:

Access-Control-Allow-Origin : http://www.wombling.com
Access-Control-Allow-Credentials : true

重要的是,OPTIONS调用的响应头中只有一个允许的“origin”,而不是“*”。我通过从请求中读取源并将其填充回响应来实现这一点——可能绕过了限制的最初原因,但在我的用例中,安全性并不是最重要的。

我认为有必要明确地提到只有一个源的要求,因为W3C标准允许空格分隔列表,但Chrome不允许! http://www.w3.org/TR/cors/#access-control-allow-origin-response-header 注意“在实践中”的部分。

我在跨域场景下操作。在登录期间,远程服务器返回set - cookie报头,并将Access-Control-Allow-Credentials设置为true。

下一次对远程服务器的ajax调用应该使用这个cookie。

CORS的Access-Control-Allow-Credentials允许跨域日志记录。查看https://developer.mozilla.org/En/HTTP_access_control获取示例。

对我来说,这似乎是JQuery的一个bug(或者至少是下一个版本的新特性)。

更新:

cookie不会从AJAX响应中自动设置(引用:http://aleembawany.com/2006/11/14/anatomy-of-a-well-designed-ajax-login-experience/) 为什么? 您无法从响应中获取cookie的值以手动设置(http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-getresponseheader) 我困惑. . 应该有一种方法可以让jquery.ajax()设置XMLHttpRequest。withCredentials = "true"参数。

答: 您应该使用http://api.jquery.com/jQuery.ajax/的xhrFields参数

文档中的示例如下:

$.ajax({
   url: a_cross_domain_url,
   xhrFields: {
      withCredentials: true
   }
});

服务器正确地响应这个请求也很重要。这里复制来自@Frédéric和@Pebbl的精彩评论:

重要提示:当响应一个已验证的请求时,服务器必须指定一个域,并且不能使用通配符。如果头部是通配符,上面的例子将失败:Access-Control-Allow-Origin: *

所以当请求是:

Origin: http://foo.example
Cookie: pageAccess=2

服务器应响应:

Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Credentials: true

[payload]

否则有效负载将不会返回到脚本。见:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Requests_with_credentials