這個問題屬於Ajax跨域攜帶Cookie的問題,找了一篇博文的解決方案。
在http://www.xxx.com域下發起對xxx.com的一個請求
原生ajax請求方式:
var xhr = new XMLHttpRequest(); xhr.open("POST", "http://xxxx.com/demo/b/index.php", true); xhr.withCredentials = true; //支持跨域發送cookies xhr.send();
jquery的post方法請求:
$.ajax({
type: "POST", url: "http://xxx.com/api/test", dataType: 'jsonp', xhrFields: {withCredentials: true}, crossDomain: true, })
服務器端設置:
header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Origin: http://www.xxx.com");
上面的Access-Control-Allow-Origin不可以設置為*,且cookie所在domain必須既是www.xxx.com的上級域名或者本級域名,也是xxx.com的上級域名或者本級域名。
轉自http://www.cnblogs.com/10-8-2016-song/p/6290492.html
