Jquery Ajax CORS +仅 Http Cookie

发布于 2024-11-18 10:18:58 字数 798 浏览 1 评论 0原文

我已经在我当前的项目中使用了 CORS,尽管我似乎无法正常工作的一件事是 cookie。

现在我得到了 cookie,服务器发出它并将其发送下来,firefox 接受它,我可以在 firebug cookies 部分看到它。然而,当我对该服务进行后续调用时,它似乎没有在标头中发送 cookie...

GET /some/entity/ HTTP/1.1
Host: localhost:1837
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: */*
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://localhost:6879
Origin: http://localhost:6879

我需要对我的 ajax 调用执行任何特殊操作吗?

var ajaxOptions = {
    url: serviceResourceUrl,
    type: "get",
    dataType: "json",
    success: successCallback,
    error: failedCallback,
    xhrFields: { withCredentials: true }
};

$.ajax(ajaxOptions);

I have got CORS working on my current project, although one thing I cannot seem to get working correctly is the cookies.

Now I get the cookie fine, the server issues it and sends it down and firefox accepts it, I can see it in the firebug cookies section. However when I make subsequent calls to that service it doesnt seem to send the cookie in the header...

GET /some/entity/ HTTP/1.1
Host: localhost:1837
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: */*
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://localhost:6879
Origin: http://localhost:6879

Do I need to do anything special with my ajax call?

var ajaxOptions = {
    url: serviceResourceUrl,
    type: "get",
    dataType: "json",
    success: successCallback,
    error: failedCallback,
    xhrFields: { withCredentials: true }
};

$.ajax(ajaxOptions);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2024-11-25 10:18:58

尝试使用 beforeSend 属性而不是 xhrFields。对于您的情况:

var ajaxOptions = {
    url: serviceResourceUrl,
    type: "get",
    dataType: "json",
    success: successCallback,
    error: failedCallback,
    beforeSend: function(xhr){
      xhr.withCredentials = true;
    }
};

$.ajax(ajaxOptions);

您可以在此处了解更多信息:使用跨域帖子发送凭据?

Try using the beforeSend property instead of xhrFields. In your case:

var ajaxOptions = {
    url: serviceResourceUrl,
    type: "get",
    dataType: "json",
    success: successCallback,
    error: failedCallback,
    beforeSend: function(xhr){
      xhr.withCredentials = true;
    }
};

$.ajax(ajaxOptions);

You can learn more here: Sending credentials with cross-domain posts?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文