通过Ajax Call传递Cookie或在PHP中使用$ _COOKIE

时间:2017-12-20 19:12:50

标签: javascript php jquery ajax cookies

是否有一个特定的原因,为什么通过AJAX调用传递cookie会对性能或维护代码产生影响或不会产生影响,而不是简单地在PHP内部调用$_COOKIE

在这种情况下,我使用此插件在javascript中获取Cookie:Cookie Plugin

通过Ajax调用传递cookie的示例:

$.ajax({
    url: 'getCaseManagerReport.php',
    method: 'POST',
    dataType: 'json',
    async: true,
    data: {activeDateSelected: Cookies.get('activeDateSelected').replace(/[+]/g, " "), activeProductSelected: Cookies.get('activeProductSelected')}
})
.done(function(response) {
    //do stuff
})
.fail(function(jqXHR, textStatus, errorThrown) {
    alert("There was a problem during verification. Please contact your System Administrator...");
    console.log(jqXHR);
    console.log(textStatus);
    console.log(errorThrown);
});

});

然后在getCaseManagerReport.php

$activeDateSelected = $_POST[‘activeDateSelected’];

VS使用$_COOKIE检索Cookie

$activeDateSelected = $_COOKIE[‘activeDateSelected’];

0 个答案:

没有答案