ajax 发送cookiecookie 丢失,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');

根据自己的情况而定