跨域请求怎么携带cookie

前端

$.ajax({
url : ‘http://remote.domain.com/corsrequest’,
data : data,
dataType: ‘json’,
type : ‘POST’,
xhrFields: {
withCredentials: true
},
crossDomain: true,
contentType: “application/json”,
})
通过设置 withCredentials: true ,发送Ajax时,Request header中便会带上 Cookie 信息。

后台

对应客户端的 xhrFields.withCredentials: true 参数,服务器端通过在响应 header 中设置 Access-Control-Allow-Credentials = true 来运行客户端携带证书式访问。通过对 Credentials 参数的设置,就可以保持跨域 Ajax 时的 Cookie。这里需要注意的是:

服务器端 Access-Control-Allow-Credentials = true时,参数Access-Control-Allow-Origin 的值不能为 ‘*’ 。

猜你喜欢

转载自blog.csdn.net/Admin_yws/article/details/123256416