jQuery设置cookie来发布方法

问题描述 投票:1回答:2

如何在jQuery ajax方法中将cookie设置为post方法?

我的代码是:

$.ajax({
    type: "POST",
    url: "https://myUrl",
    data: "data",
    contentType: "application/x-www-form-urlencoded",
});

我想把cookie放在那里,但不是从当前会话,就像String(我从其他网站得到它),试图添加如下:

cookie: "myCookies",

但它不起作用。

我该如何解决这个问题?

javascript jquery html ajax cookies
2个回答
0
投票

您无法在跨域请求上设置自定义cookie,请尝试使用此代码。它设置了自己域名的cookie:

$.ajax({
   type: "POST",
   url: "URL",
   data: "data",
   xhrFields: {
      withCredentials: true
   },
   contentType: "application/x-www-form-urlencoded"
});

0
投票

为什么你想要那个cookie?

如果您只想跟踪当前会话中的数据,可以使用sessionStorage或localStorage。

© www.soinside.com 2019 - 2024. All rights reserved.