原理:代碼允許攜帶cookie
操作:
A.後端代碼加header
header("Access-Control-Allow-Origin:www.aaa.com"); //允許跨域位址(*,域名,ip)
header('Access-Control-Allow-Credentials:true'); //允許用戶端攜帶cookie,注意在此處為true時,上面一行域名不能設定為*,隻能放域名或ip位址
header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
B.前端ajax增加攜帶cookie
$.ajax({
type: "POST",
crossDomain: true, //允許跨域請求
xhrFields:{
withCredentials:true //跨域攜帶cookie
},
...
...
...
})