簡體   English   中英

無法在不同的域上設置 cookie

[英]Can't set cookie on different domain

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

哪個應該接受 URL 中傳遞的變量。 然后將其作為 cookie go.allthatstrendy.com保存到此域。 這是通過 AJAX 執行的 PHP 腳本完成的。

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.

運行上面的URL后。 Go 到https://go.allthatstrendy.com/intercart/並檢查 cookies。 它沒有設置!

我什至設置了它,因此您可以直接在 go.allthatstrendy.com 上執行腳本並直接在那里設置 cookie。 它就是這樣工作的。

請參閱: https://go.allthatstrendy.com/intercart/cookies/Test/saveCookies2.php?UTMParamsString=TESTjhghgjghj

但是,當我嘗試在此處設置 cookie 時https://trywifibooster.com/test/setCookiesFromAnotherDomain.html?param=SHOULD-SET-TO-THIS

它沒有設置它。 沒有跨域錯誤或任何東西。 我已經來回走了3個多小時,老實說,我要尖叫了。 這沒有道理。 我是一位經驗豐富的開發人員。 所以更讓人郁悶!

XHR 不會發送或接受 cookies 除非您明確啟用憑證支持:

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

請注意,這將使您的請求預檢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM