dplo59755 2016-10-23 17: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 18: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条)

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败