这次给大家带来ajax跨域访问时cookie丢失怎么解决,解决ajax跨域访问时cookie丢失的注意事项有哪些,下面就是实战案例,一起来看一下。
ajax跨域访问,可以使用jsonp方法或设置access-control-allow-origin实现,关于设置access-control-allow-origin实现跨域访问可以参考之前我写的文章《ajax 设置access-control-allow-origin实现跨域访问》
1.ajax跨域访问,cookie丢失
首先创建两个测试域名
a.fdipzone.com 作为客户端域名
b.fdipzone.com 作为服务端域名
测试代码
setcookie.php 用于设置服务端cookie
<?php
setcookie('data', time(), time()+3600);
?>
server.php 用于被客户端请求
<?php
$name = isset($_post['name'])? $_post['name'] : '';
$ret = array(
'success' => true,
'name' => $name,
'cookie' => isset($_cookie['data'])? $_cookie['data'] : ''
);
// 指定允许其他域名访问
header('access-control-allow-origin:http://a.fdipzone.com');
// 响应类型
header('access-control-allow-methods:post');
// 响应头设置
header('access-control-allow-headers:x-requested-with,content-type');
header('content-type:application/json');
echo json_encode($ret);
?>
test.html 客户端请求页面
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<title> ajax 跨域访问cookie丢失的解决方法 </title>
</head>
<body>
<script type="text/javascript">
$(function(){
$.ajax({
url: 'http://b.fdipzone.com/server.php', // 跨域
datatype: 'json',
type: 'post',
data: {'name':'fdipzone'},
success:function(ret){
if(ret['success']==true){
alert('cookie:' + ret['cookie']);
}
}
});
})
</script>
</body>
</html>
首先先执行http://b.fdipzone.com/setcookie.php, 创建服务端cookie。
然后执行http://a.fdipzone.com/test.html
输出
{success:true,name:fdipzone,cookie:}
获取cookie失败。
2.解决方法
客户端
请求时将withcredentials属性设置为true
使可以指定某个请求应该发送凭据。如果服务器接收带凭据的请求,会用下面的http头部来响应。
服务端
设置header
header(access-control-allow-credentials:true);
允许请求带有验证信息
test.html 修改如下
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<title> ajax 跨域访问cookie丢失的解决方法 </title>
</head>
<body>
<script type="text/javascript">
$(function(){
$.ajax({
url: 'http://b.fdipzone.com/server.php', // 跨域
xhrfields:{withcredentials: true}, // 发送凭据
datatype: 'json',
type: 'post',
data: {'name':'fdipzone'},
success:function(ret){
if(ret['success']==true){
alert('cookie:' + ret['cookie']);
}
}
});
})
</script>
</body>
</html>
server.php 修改如下
<?php
$name = isset($_post['name'])? $_post['name'] : '';
$ret = array(
'success' => true,
'name' => $name,
'cookie' => isset($_cookie['data'])? $_cookie['data'] : ''
);
// 指定允许其他域名访问
header('access-control-allow-origin:http://a.fdipzone.com');
// 响应类型
header('access-control-allow-methods:post');
// 响应头设置
header('access-control-allow-headers:x-requested-with,content-type');
// 是否允许请求带有验证信息
header('access-control-allow-credentials:true');
header('content-type:application/json');
echo json_encode($ret);
?>
按之前步骤执行,请求返回
{success:true,name:fdipzone,cookie:1484558863}
获取cookie成功
3.注意事项
1.如果客户端设置了withcredentials属性设置为true,而服务端没有设置access-control-allow-credentials:true,请求时会返回错误。
xmlhttprequest cannot load http://b.fdipzone.com/server.php. credentials flag is 'true', but the 'access-control-allow-credentials' header is ''. it must be 'true' to allow credentials. origin 'http://a.fdipzone.com' is therefore not allowed access.
2.服务端header设置access-control-allow-credentials:true后,access-control-allow-origin不可以设为*,必须设置为一个域名,否则回返回错误。
xmlhttprequest cannot load http://b.fdipzone.com/server.php. a wildcard '*' cannot be used in the 'access-control-allow-origin' heade
下面看下ajax跨域请求cookie无法带上的解决办法
原生ajax请求方式:
var xhr = new xmlhttprequest();
xhr.open(post, http://xxxx.com/demo/b/index.php, true);
xhr.withcredentials = true; //支持跨域发送cookies
xhr.send();
jquery的ajax的post方法请求:
$.ajax({
type: post,
url: http://xxx.com/api/test,
datatype: 'jsonp',
xhrfields: {
withcredentials: true
},
crossdomain: true,
success:function(){
},
error:function(){
}
})
服务器端设置:
header(access-control-allow-credentials: true);
header(access-control-allow-origin: http://www.xxx.com);
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
用ajax实现注册与头像上传功能
使用ajax实现分页技术的步奏详解(附代码)
在ajax的请求中async:false与async:true有什么区别
以上就是ajax跨域访问时cookie丢失怎么解决的详细内容。