dplo59755 2016-10-23 09:53
浏览 43
已采纳

当使用ajax将请求发送到服务器时,我无法检索$ _COOKIE ['mycookiename']的值

I want to send something to server with ajax GET method and also using cookies, so I send my value to ajax, I need this value to update the current cookie value. In ajax file, I can't retrieve $_COOKIE['cookie_name'] , when I echo this, the result will be like this--> [] !

function card(action,value) {
    if (value == "") {
        document.getElementById('errmsgs').innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById('errmsgs').innerHTML = xmlhttp.responseText;
            }
        };

        xmlhttp.open("GET","inc/pricing/pricing.php?act="+action+"&val="+value,true);
        xmlhttp.send();
    }
}

And the pricing page is:

<?php 
require_once '../../connection.php';
require_once '../../func.php';

$what_is_action = $_REQUEST['act']; 
$myCook = $_REQUEST['val'];

}
echo $full_cookie_value = $_COOKIE['mycookiename']; ----> output will be--> [] ---->but it must be something like this ---> {"wwde34":"1","effy33":"1","ssdfff":"1"}


if($what_is_action == 'delete'){
    //delete_cook($myCook);
}
?>

Solved.
The problem was, when I trying to create the cookie, it was not in root, so in another folders It can't be accessible! so I added '/' at the last parameter of setcookie for example: setcookie('a','b',time()+36666,'/') this will create cookie in root and it can be accessible in other folders!

展开全部

  • 写回答

2条回答 默认 最新

  • dongti8535 2016-10-23 10:08
    关注

    Has your cookie been made?

    Make sure the cookie is set! Example:

    Where you see the I button you will need to press it and find the cookie and capsulses, check if you can find your cookie in there! This must be done from your website.

    Getting the error

    try
    {
        echo $_COOKIE['name']['bills'];
    }
    catch (Throwable $t) // PHP 7
    {
        echo '<b>COOKIE is not made</b>';
    }
    catch (Exception $e) // PHP 5
    {
        echo '<b>COOKIE is not made</b>';
    }
    

    Please have a look at : Getting cookies with javascript

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部