我现在有个需求就是跳转静态页面的时候需要在请求头中带上cookie,可以用window.location.href方法跳转吗

我现在有个需求就是跳转静态页面的时候需要在请求头中带上cookie

我现在有个需求就是跳转静态页面的时候需要在请求头中带上cookie,可以用window.location.href方法跳转吗?可以的话请问怎么做才行呢?

同域名可以,在js里写cookie就行,cookie 是整站通用的

如果是不同域名的,那就不能用 location.href 带cookie了

GRT是这样说的,这个我也不确定,你试试:

// 获取cookie的值
var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)cookieName\s*\=\s*([^;]*).*$)|^.*$/, "$1");

// 创建xhr对象并发送同步请求
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com/staticpage.html", false);  // 同步请求
xhr.setRequestHeader("Cookie", "cookieName=" + encodeURIComponent(cookieValue));
xhr.send();

// 跳转到静态页面
window.location.href = "http://example.com/staticpage.html";