本文主要是从前端jquery和服务端PHP为例,分别使用实例解决ajax跨域请求数据cookie丢失问题,推荐给有相同需求的小伙伴们。
前端:
以jquery为例:
需要加入
xhrFields: {
withCredentials: true
},crossDomain: true,$.ajax({ type: postType,url: url,data: postData || '',xhrFields: { withCredentials: true },success: function () { successCallback.apply(scope || this,arguments); },failureCallback: () { failureCallback.apply(scope || 服务器端:
以PHP为例:
header("Access-Control-Allow-Credentials
: true");
header('Access-Control-Allow-Origin: http://m.z.com');
根据自己的情况而定
以上所述就是本文关于解决ajax跨域请求数据时cookie丢失的处理方案,希望大家能够喜欢