jQuery 会在帖子中发送 cookie 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1041285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:29:01  来源:igfitidea点击:

Does jQuery send cookies in a post?

jquerycookiespost

提问by ramayac

I want to know if, when I make a $.post(...)with jQuery, is any cookie sent to the server in the post request?

我想知道,当我使用 jQuery制作$.post(...)时,是否有任何 cookie 在 post 请求中发送到服务器?

Thanks!

谢谢!

回答by Ayman Hourieh

Cookies are sent with Ajax requests.

Cookie 随 Ajax 请求一起发送。

When the HTTPOnlyflag is set for a cookie, this cookie is hidden from client-side scripts, but the cookie is still sent with Ajax requests.

当为 cookie 设置HTTPOnly标志时,此 cookie 对客户端脚本是隐藏的,但 cookie 仍与 Ajax 请求一起发送。

回答by Chris Simpson

Using the same cookies on the client and the server is not possible when you have httpOnlyCookies switched on. There is very good reason switch this on too:

当您打开 httpOnlyCookies 时,无法在客户端和服务器上使用相同的 cookie。也有很好的理由打开它:

http://www.codinghorror.com/blog/archives/001167.html

http://www.codinghorror.com/blog/archives/001167.html

回答by MSpreij

Using Firefox+Firebug you can see exactly what jQuery sends, and how. Useful for debugging!

使用 Firefox+Firebug,您可以准确查看 jQuery 发送的内容以及发送方式。对调试有用!

回答by briznad

Sorry to be a wet blanket, but I'm going to contradict the positive vibes here and say NO.

很抱歉成为一个湿毯子,但我将与这里的积极氛围相矛盾并说不。

I'm currently building an app using $.post to connect to my API backend, which is powered by Expressand node.js. I'm using the Express cookie parser middleware to read cookies sent over in each request. If I hit my endpoint directly via the browser the backend server can see the cookies visible on my domain. However, when I use $.post in my app the cookie object is just blank.

我目前正在使用 $.post 构建一个应用程序来连接到我的 API 后端,该后端由Expressnode.js 提供支持。我正在使用 Express cookie 解析器中间件来读取每个请求中发送的 cookie。如果我直接通过浏览器点击我的端点,后端服务器可以看到我的域中可见的 cookie。但是,当我在我的应用程序中使用 $.post 时,cookie 对象只是空白。

It's possible I'm missing something but I've been testing this for the last couple hours and the conclusion I've come to is that cookies are simply not sent using a jQuery $.post request :/

我可能遗漏了一些东西,但我在过去的几个小时里一直在测试这个,我得出的结论是 cookie 根本不是使用 jQuery $.post 请求发送的:/

回答by glyph

One thing to consider is cookie path. If an ajax-loaded script sets a cookie then its path may be different than the parent page, putting it in a different scope for some server applications or JQuery. I spent a while today spinning my wheels on this then noticed the cookies I was having trouble reading had a different path set.

需要考虑的一件事是 cookie 路径。如果一个 ajax 加载的脚本设置了一个 cookie,那么它的路径可能与父页面不同,对于某些服务器应用程序或 JQuery,将其放在不同的范围内。今天我花了一段时间在这个上旋转我的轮子,然后注意到我在阅读时遇到困难的饼干设置了不同的路径。

Simple fix for me was to set the path of all cookies to / with jquery in my ajax request like so:

对我来说,简单的解决方法是在我的 ajax 请求中使用 jquery 将所有 cookie 的路径设置为 / ,如下所示:

 $.cookie("isolates_grid_tgl", "true", { path: "/" });

firebug cookies view

萤火虫饼干视图