dongqi3533 2019-07-28 12:26
浏览 103

使用JS发布时,php setcookie不起作用

I have a strange problem- In my website https://www.example.com I have this JS code, triggered on some JS event:

$.post( 'https://www.example2.com/api/save_cookie.php',
    { storageData: storageData },
    function(data) {console.log(data)}
);

In https://www.exmaple2.com, the file save_cookie.php looks like this:

$test = setcookie('test_cookie', '1', time() + (10 * 365 * 24 * 60 * 60), '/');
echo $test;

When I trigger the JS event, I can see in the network tab that everything is okay, that the request sent successfully to the url https://www.exmaple2.com/api/save_cookie.php, and I can see "1" printed in the console, from the callback function.

But in https://www.exmaple2.com, $_COOKIE['test_cookie'], is null. However, If I just browse the url www.exmaple2.com/api/save_cookie.php, and check the variable $_COOKIE['test_cookie'] after that, I can see that the value is '1', as expected. Any ideas why the cookie is not set when posting the request with ajax?

Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?