简体   繁体   English

无法在不同的域上设置 cookie

[英]Can't set cookie on different domain

This URL has the code below: https://trywifibooster.com/test/setCookiesFromAnotherDomain.html?param=SHOULD-SET-TO-THIS这个 URL 的代码如下: https://trywifibooster.com/test/setCookiesFromAnotherDomain.htmlISparam=SHO

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);

Which should take in the variables passed in the URL.哪个应该接受 URL 中传递的变量。 Then save it to this domain as a cookie go.allthatstrendy.com .然后将其作为 cookie go.allthatstrendy.com保存到此域。 It's done through a PHP script executed by AJAX.这是通过 AJAX 执行的 PHP 脚本完成的。

The PHP script: 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'];

?>

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

After running the URL above.运行上面的URL后。 Go to https://go.allthatstrendy.com/intercart/ and check the cookies. Go 到https://go.allthatstrendy.com/intercart/并检查 cookies。 It's not set!它没有设置!

I've even set it up so you can execute a script directly on go.allthatstrendy.com and set the cookie directly there.我什至设置了它,因此您可以直接在 go.allthatstrendy.com 上执行脚本并直接在那里设置 cookie。 It works like that.它就是这样工作的。

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

However, when I try and set the cookie here https://trywifibooster.com/test/setCookiesFromAnotherDomain.html?param=SHOULD-SET-TO-THIS但是,当我尝试在此处设置 cookie 时https://trywifibooster.com/test/setCookiesFromAnotherDomain.html?param=SHOULD-SET-TO-THIS

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.我已经来回走了3个多小时,老实说,我要尖叫了。 It makes no sense.这没有道理。 I am an experienced developer.我是一位经验丰富的开发人员。 So it makes it even more frustrating!所以更让人郁闷!

XHR doesn't send or accept cookies unless you explicitly enable credential support: XHR 不会发送或接受 cookies 除非您明确启用凭证支持:

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

Note that this will make your request preflighted .请注意,这将使您的请求预检

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM