• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Ajax跨域访问Cookie丢失问题的解决方法

jquery 搞代码 4年前 (2021-12-27) 23次浏览 已收录 0个评论

这篇文章主要介绍了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

 

server.php 用于被客户端请求

  true, 'name' => $name, 'cookie' => isset($_COOKIE['data'])? $_COOKIE['data'] : '' ); // 指定允许其他域名访问 header('Access-<mark style="color:transparent">来源gaodaimacom搞#代%码网</mark>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 客户端请求页面

   <title> ajax 跨域访问cookie丢失的解决方法 </title> 

首先先执行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 修改如下

   <title> ajax 跨域访问cookie丢失的解决方法 </title> 

server.php 修改如下

  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跨域访问Cookie丢失问题的解决方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Ajax跨域访问Cookie丢失问题的解决方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址