dpa89292 2011-04-16 02:07
浏览 37

通过ajax设置和检索外部cookie

I am sending an Ajax request to a PHP script where I am using setcookie to set the cookies on an external domain. After that, I am sending another Ajax request to the external domain to read the cookies. I am unable to achieve the results. I am using jQuery to send Ajax requests.

The PHP script:

<?php
// yummy cookies ?

header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
header("Access-Control-Allow-Credentials: true");

if($_GET['read'] == true) {
    echo json_encode(
        array(
            'user_id' => $_COOKIE['user_id'],
            'twitter_name' => $_COOKIE['twitter_name'],
            'facebook_id' => $_COOKIE['facebook_id']
        )
    );
}

if($_GET['write'] == true) {
    if($_GET['type'] == 'twitter') {
        setcookie('twitter_name', $_GET['twitter_name'], time() + 1209600);
        setcookie('user_id', $_GET['user_id'], time() + 1209600);
    }

    if($_GET['type'] == 'facebook') {
        setcookie('facebook_id', $_GET['facebook_id'], time() + 1209600);
        setcookie('user_id', $_GET['user_id'], time() + 1209600);
    }
}
  • 写回答

1条回答 默认 最新

  • doutang1884 2011-04-16 04:02
    关注

    You might be running into the problem where ajax doesn't allow cross site calls (can't call to another domain).

    Most people use an iframe instead of ajax, put the src to the other site's cookie script.

    The iframe would look like <iframe src="otherdomain.com/cookiecode.php?var1=something" style="visibility:hidden;display:none"></iframe>

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?