最好的解决方法:最可以设置多个域名,也可以允许cookie跨域
$origin=isset($_SERVER['HTTP_ORIGIN'])?$_SERVER['HTTP_ORIGIN']:'';
$allowDomain=[
'http://www.yunindex.com',
'http://web.yunindex.com'
];
if(in_array($origin,$allowDomain)){
header("Access-Control-Allow-Origin:".$_SERVER['HTTP_ORIGIN']);
header("Access-Control-Allow-Credentials: true");
}
关于withCredentials:
XMLHttpRequest.withCredentials 属性是一个Boolean
类型,它指示了是否该使用类似cookies,authorization headers(头部授权)或者TLS客户端证书这一类资格证书来创建一个跨站点访问控制(cross-site Access-Control
)请求。在同一个站点下使用withCredentials属性是无效的。
此外,这个指示
也会被用做响应中
cookies 被忽视的标示。默认值是false。
如果在发送来自其他域的XMLHttpRequest请求之前,未设置withCredentials
为true,那么就不能为它自己的域设置cookie值。而通过设置withCredentials
为true获得的第三方cookies,将会依旧享受同源策略,因此不能被通过document.cookie或者从头部相应请求的脚本等访问。
注: 永远不会影响到同源请求
Note: 不同域下的XmlHttpRequest
响应,不论其Access-Control-
header 设置什么值,都无法为它自身站点设置cookie值,除非它在请求之前将withCredentials
设为true。
原文:https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/withCredentials
最近尝试发现下面这种更好,所有域名都可以访问,但是cookie不能跨域:
header('content-type:application:json;charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:*');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
以下是之前的
在test.hu-rong.com中用ajax调用www.hu-rong.com 的接口,一直http 302读不到cookie,这是由于跨域的时候读cookies要加上
xhrFields':{withCredentials: true}
否则,如果接口那边有验证cookies登录状态的话,是获取不到cookies的,这样会被重定向到登录页面,此时接口就返回状态码302;
但是加了
xhrFields:{withCredentials: true},
crossDomain: true,
后奇怪的是,接口正常返回数据,但ajax就是不进入success,而是进入了error中,后来发现这种设置必须将接口那边跨域的header头中Access-Control-Allow-Origin设置为具体域名,而不是*
接口那边设置允许跨域,然后ajax请求加上:
折腾了大半天不容易,转载请保留本文链接:http://www.hu-rong.com/article/406
示例:在user.hu-rong.com中:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<script language="javascript" src="jquery.min.js"></script>
<script>
$(function(){
$.ajax({
type:'GET',
xhrFields:{withCredentials: true},
crossDomain: true,
success:function(res){
console.log(res);
alert(2);
},
error:function(res){
alert(1);
}
});
});
</script>
</head>
<body>
</body>
</html>
接口端:
header("Access-Control-Allow-Origin:http://user.hu-rong.com");
header("Access-Control-Allow-Credentials: true");
- 相关文章
- phpexcel设置行高及列宽,背景颜色,
- 单点登录sso原理及php实现方式及de
- 在php中用curl请求java接口无法获
- 免费ip地址查询接口
- php,java,android, aes加解密-加密
- 地图上计算两点间的距离
- 关于百度地图的一些技巧
- php中empty,is_null,isset的区别
- windows及linux下composer安装教程
- 一种精妙的数据查询与存储方式
- 热门文章
- win7中将文件拷贝到虚拟机linux下
- phpexcel设置行高及列宽,背景颜色,
- rabbitmq无法启动
- intellij idea不显示git push按钮
- php7中使用mongodb的aggregate进行
- centos7.4 64位下swoole安装及配置
- laravel页面静态化的方法
- navicate连接mycat报1184错误
- 单点登录sso原理及php实现方式及de
- devops-jenkins容器为pending状态
- 好评文章
- phpexcel设置行高及列宽,背景颜色,
- php7中使用mongodb的aggregate进行
- intellij idea打开文件所在文件夹
- windows下使用MongoDB Compass Com
- win7中将文件拷贝到虚拟机linux下
- laravel 中悲观锁 & 乐观锁的使用
- 单点登录sso原理及php实现方式及de
- navicate连接mycat报1184错误
- rabbitmq无法启动
- laravel整合dingo/api方法步骤:jwt
- 我的项目
- 【github】www.github.com/hurong241
- 【码云】gitee.com/hu_rong/projects
- 【docker hub】hub.docker.com/repositories/hurong241
- 【packagist】packagist.org/users/hurong241/packages
- 站点信息
- 建站时间:2011年
- 文章数:610篇
- 浏览数:953068