相同來源請求罰款。
交叉原點請求有一些限制。
文件:1.PHP:
<?php
setcookie("cookie_name_1", "cookie_value_1", time() + (86400 * 30), "/");
?>
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://foo.ir/2.php', true);
xhr.withCredentials = true;
xhr.onreadystatechange = function() {
if(this.readyState == xhr.DONE) {
get_res();
}
}
xhr.send(null);
function get_res(){
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://foo.ir/2.php?is_set', true);
xhr.withCredentials = true;
xhr.onload = function() {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
console.log(xhr.responseText);
}
}
};
xhr.send(null);
}
</script>
文件:2.PHP
<?php
header('Access-Control-Allow-Origin: http://localhost');
header('Access-Control-Allow-Credentials: true');
if(isset($_GET["is_set"])){
if(isset($_COOKIE["cookie_name_2"]))
echo "cookies are set:".$_COOKIE["cookie_name_2"];
else
echo "cookies not set";
}else
setcookie("cookie_name_2", "cookie_value_2", time() + (86400 * 30), "/");
?>
- 跨域餅乾工作:您需要允許第三方Cookie設置在 瀏覽器設置
- 我找不到第三方cookie被存儲。
- Chrome瀏覽器不顯示cookies,不會影響真實網站。
- Firefox &邊緣cookie保存在第三方網站存儲,因此它會影響真正的第三方網站。
更多信息,請Here
中找到根據了XMLHttpRequest Level 1和XMLHttpRequest的2級, 這個特殊的響應頭瀑布下的「禁止」響應 頭,你可以得到使用getResponseHeader (),所以只能 原因,這可能是工作基本上是一個「調皮」的瀏覽器
能否請您看一下https://developer.mozilla.org/en-US/docs/Web/API/X MLHttpRequest/withCredentials第二段? –
@MahdiRafatjah感謝您的鏈接。 'withCredentials'只能引用CORS(巧合的是,它在上面的XHR請求顯示中也已經設置爲true)。相反,我想知道哪些地方可以在Chrome開發工具中看到這個XHR cookie值。 – AJP