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 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题