无法在其他域上设置Cookie [英] Can't set cookie on different domain

查看:89
本文介绍了无法在其他域上设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此URL包含以下代码: https://trywifibooster.com/test/setCookiesFromAnotherDomain.html?param=SHOULD-SET-TO-THIS

This URL has the code below: https://trywifibooster.com/test/setCookiesFromAnotherDomain.html?param=SHOULD-SET-TO-THIS

var params = new window.URLSearchParams(window.location.search).get('param');

$.ajax({

  type: 'GET',
  crossDomain: true,
  url: 'https://go.allthatstrendy.com/intercart/cookies/Test/saveCookies2.php',
  data: 
  "UTMParamsString=" + params,

  //success
  success:function(data) {

    console.log(data);

  },
  //error
  error:function(xhr, options, error) {
     console.log("Cookies not successfully saved" + error);
  }
});

alert("Sent: " + params);

应采用URL中传递的变量.然后将其另存为Cookie go.allthatstrendy.com 到此域.这是通过AJAX执行的PHP脚本完成的.

Which should take in the variables passed in the URL. Then save it to this domain as a cookie go.allthatstrendy.com. It's done through a PHP script executed by AJAX.

PHP脚本:

<?php

    // Headers
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Allow-Methods: GET, POST");
    header("Access-Control-Allow-Headers: Content-Type, *");

    if(isset($_GET['UTMParamsString'])) {

        $UTMParamsString = $_GET['UTMParamsString'];

        setcookie("UTMParamsString", $UTMParamsString, time()+3600, "/", "allthatstrendy.com", 1);

    }

    echo "GET VARIABLE: " . $UTMParamsString;

    echo "<br/>";

    echo "CHECK COOKIE WAS SET: " . $_COOKIE['UTMParamsString'];

?>

但是,执行trywifibooster.com上的Ajax导致go.allthatstrendy.com时,没有设置cookie.

However, when the Ajax on trywifibooster.com is executed, leading to go.allthatstrendy.com, no cookies are set.

运行上面的URL之后.转到 https://go.allthatstrendy.com/intercart/并检查Cookie.没有设置!

After running the URL above. Go to https://go.allthatstrendy.com/intercart/ and check the cookies. It's not set!

我什至设置了它,因此您可以直接在go.allthatstrendy.com上执行脚本,并直接在其中设置cookie.就是这样.

I've even set it up so you can execute a script directly on go.allthatstrendy.com and set the cookie directly there. It works like that.

请参阅: https://go.allthatstrendy.com/intercart/cookies/Test/saveCookies2.php?UTMParamsString=TESTjhghgjghj

但是,当我尝试在此处设置cookie时 https://trywifibooster.com/test/setCookiesFromAnotherDomain.html?param = SHOULD-SET-TO-THIS

However, when I try and set the cookie here https://trywifibooster.com/test/setCookiesFromAnotherDomain.html?param=SHOULD-SET-TO-THIS

它没有设置.没有跨域错误或其他任何错误.我来回走了3个多小时,老实说,我要尖叫了.这个不成立.我是一个经验丰富的开发人员.因此,这更加令人沮丧!

It doesn't set it. There is no cross-origin error or anything. I've gone back and forth for over 3 hours and I'm honestly about to scream. It makes no sense. I am an experienced developer. So it makes it even more frustrating!

推荐答案

除非明确启用凭据支持,否则XHR不会发送或接受cookie:

XHR doesn't send or accept cookies unless you explicitly enable credential support:

$.ajax({
  type: 'GET',
  xhrFields: {
    withCredentials: true
  }

请注意,这将使您的请求没事.

Note that this will make your request preflighted.

这篇关于无法在其他域上设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆