web前端|js教程
ajax,跨域
web前端-js教程
activex控件 源码,vscode 保存即格式化,ubuntu美化macos,tomcat导出乱码问题,ffmpeg网络爬虫,php开发硬件,百度seo软件推广工具,wap网站模板下载lzw
ajax跨域访问,可以使用jsonp方法或设置Access-Control-Allow-Origin实现,关于设置Access-Control-Allow-Origin实现跨域访问可以参考之前我写的文章《ajax 设置Access-Control-Allow-Origin实现跨域访问》
移动云分销源码2.4,公司要求用ubuntu,拿到tomcat包怎么起服务,爬虫标志的鞋,径赢网络科技php,松江seo课程lzw
1.ajax跨域访问,cookie丢失
tinder 源码,vscode登录mysql,lxqt ubuntu,tomcat 设置图片,安卓基于sqlite,jquery jcrop插件截图使用方法,怎么查看代码的前端框架,爬虫批量生成nft头像,帝国模板 php,象山seo优化方式,dede网站数据库路径,网页制作三级素材,淘宝店铺本店搜索模板lzw
首先创建两个测试域名
作为客户端域名
作为服务端域名
测试代码
setcookie.php 用于设置服务端cookie
server.php 用于被客户端请求
true, ame => $name, cookie => isset($_COOKIE[data])? $_COOKIE[data] : \);// 指定允许其他域名访问header(Access-Control-Allow-Origin:);// 响应类型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 客户端请求页面
首先先执行/setcookie.php, 创建服务端cookie。
然后执行/test.html
输出
{"success":true,"name":"fdipzone","cookie":""}
获取cookie失败。
2.解决方法
客户端
请求时将withCredentials属性设置为true
使可以指定某个请求应该发送凭据。如果服务器接收带凭据的请求,会用下面的HTTP头部来响应。
服务端
设置header
header("Access-Control-Allow-Credentials:true");
允许请求带有验证信息
test.html 修改如下
server.php 修改如下
true, ame => $name, cookie => isset($_COOKIE[data])? $_COOKIE[data] : \);// 指定允许其他域名访问header(Access-Control-Allow-Origin:);// 响应类型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 /server.php. Credentials flag is rue, but the Access-Control-Allow-Credentials header is \. It must be rue to allow credentials. Origin \ is therefore not allowed access.
2.服务端header设置Access-Control-Allow-Credentials:true后,Access-Control-Allow-Origin不可以设为*,必须设置为一个域名,否则回返回错误。
XMLHttpRequest cannot load /server.php. A wildcard * cannot be used in the Access-Control-Allow-Origin header when the credentials flag is true. Origin \ is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.
本篇文章讲解了ajax跨域访问cookie丢失的解决方法,更多相关内容请关注php中文网。
关于mysql explain中key_len的计算方法讲解
如何通过php 使用curl模拟ip和来源进行访问
通过mysql 转换NULL数据方法
如果觉得《关于ajax跨域访问cookie丢失的操作的讲解》对你有帮助,请点赞、收藏,并留下你的观点哦!