通过XHR请求加载域cookie

时间:2017-10-29 13:02:32

标签: javascript php cookies xmlhttprequest

当我向网址发送XHR请求时,如何在我的PHP脚本中获取Cookie:

示例:

var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    xhr.open("POST", 'http://127.0.0.1/index.php', true);

对于http://127.0.0.1我有很多饼干。但是当发送请求时,我无法抓住cookie。

1 个答案:

答案 0 :(得分:0)

实际上不可能像这样通过XHR请求从浏览器中获取cookie,因为呼叫直接发送到服务器,但cookie在浏览器中。

执行此操作的唯一方法是以类似方式生成Iframe:

var iframe = document.createElement('iframe');
iframe.frameBorder = 0;
iframe.width = 1;
iframe.height = 1;
iframe.name= "stlconv";
iframe.scrolling = "no";
iframe.allowtransparency = "true";
iframe.vspace = "0";
iframe.hspace = "0";
iframe.marginwidth = "0";
iframe.marginheight = "0";
iframe.src = url;
document.body.appendChild(iframe);

在这种情况下,您再次在浏览器中打开了HTML元素,您可以捕获cookie。

相关问题