如何在jQuery ajax方法中将cookie设置为post方法?
我的代码是:
$.ajax({
type: "POST",
url: "https://myUrl",
data: "data",
contentType: "application/x-www-form-urlencoded",
});
我想把cookie放在那里,但不是从当前会话,就像String
(我从其他网站得到它),试图添加如下:
cookie: "myCookies",
但它不起作用。
我该如何解决这个问题?
您无法在跨域请求上设置自定义cookie,请尝试使用此代码。它设置了自己域名的cookie:
$.ajax({
type: "POST",
url: "URL",
data: "data",
xhrFields: {
withCredentials: true
},
contentType: "application/x-www-form-urlencoded"
});
为什么你想要那个cookie?
如果您只想跟踪当前会话中的数据,可以使用sessionStorage或localStorage。