php+ajax单点登录实现方式
解决ajax跨源的问题
在php服务端中加入
header("Access-Control-Allow-Origin: *"); //允许所有域名的脚本访问该资源
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE"); //允许的方式,一般为POST和GET
header('Access-Control-Allow-Headers:x-requested-with,content-type');//允许的header
使浏览器携带cookie请求
在ajax里添加withCredentials的配置,允许其请求携带cookie信息。通过设置withCredentials=true,发送Ajax时,Request header中便会带上 Cookie 信息
注意在使用withCredentials=true时候对应ajax要改为异步请求,即的async要设置为true。
ajax代码
$.ajax({
type:"POST",
data:{userid:'admin'},
xhrFields:{
withCredentials:true
},
async:false,
url:"http://127.0.0.1/Interfaces/Sso/sso",
dataType:"json",
success:function(){
alert(1);
}
})