HTML5下通过response header解决跨域AJAX cookie的问题

时间:2023-03-08 18:03:42
HTML5下通过response header解决跨域AJAX  cookie的问题

ajax:

通过给Response Header添加Access-Control-Allow-Origin:*  来解决跨域请求,*代表允许所有的跨域请求,或者把*换成指定的域名

cookie:

服务端需要在Response Header中加入头

Access-Control-Allow-Credentials:true

然后客户端的ajax的withCredentials属性设置为True。  jquery中设置如下:

$.ajax({
url:"B.abc.com",
xhrFields:{
withCredentials:true
},
crossDomain:true
});

以上方式必须在IE11或以上浏览器上才能支持