Solve the problem of Ajax cross domain request data cookie loss

Time:2021-3-5

front end:

Take jQuery as an example

Need to join

Copy codeThe code is as follows:
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
    });

Server side:

Take PHP as an example

Copy codeThe code is as follows:
header(“Access-Control-Allow-Credentials: true”);
header(‘Access-Control-Allow-Origin: http://m.z.com’);

It depends on your own situation

The above is the solution to solve the problem of cookie loss when Ajax requests data across domains. I hope you like it.