本文主要是从前端jquery和服务端php为例,分别使用实例解决ajax跨域请求数据cookie丢失问题,推荐给有相同需求的小伙伴们。
前端:
以jquery为例:
需要加入
xhrFields: {
withCredentials: true
},
crossDomain: true,
$.ajax({
type: postType,
url: url,
data: postData || '',
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function () {
successCallback.apply(scope || this, arguments);
},
failureCallback: function () {
failureCallback.apply(scope || this, arguments);
},
dataType: dataType
});服务器端:
以php为例:
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: http://m.z.com');根据自己的情况而定
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号