Does jQuery send cookies in a post?

前端 未结 5 1864
执念已碎
执念已碎 2020-12-09 07:42

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

Thanks!

相关标签:
5条回答
  • 2020-12-09 08:25

    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:

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

    0 讨论(0)
  • 2020-12-09 08:27

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

    0 讨论(0)
  • 2020-12-09 08:27

    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 Express and 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.

    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 :/

    0 讨论(0)
  • 2020-12-09 08:33

    Cookies are sent with Ajax requests.

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

    0 讨论(0)
  • 2020-12-09 08:38

    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.

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

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

    firebug cookies view

    0 讨论(0)
提交回复
热议问题