0

The POST and GET data are done very easily with ajax. I can't find a way to 'include' a cookie. How can I include a 'cookie'?

Nicky Smits
  • 2,406
  • 3
  • 17
  • 23
  • possible duplicate of [jquery ajax call not sending cookie](http://stackoverflow.com/questions/7911710/jquery-ajax-call-not-sending-cookie) – Shailendra Sharma Sep 11 '15 at 09:54
  • Or this one http://stackoverflow.com/questions/2870371/why-is-jquerys-ajax-method-not-sending-my-session-cookie – intekhab Sep 11 '15 at 09:55

1 Answers1

1

There is no way to explicitly add a cookie to an XMLHttpRequest request (or for any of the other techniques used for Ajax). The setRequestHeader method explicitly forbids setting cookies.

You need to add a cookie through the normal methods (i.e. via the HTTP Set-Cookie response header and the JS document.cookie API) and then make the request.

You'll also need to set xml.withCredentials = true; for a cross-origin request (and the cookie will need to belong to the host you are making the request to, not the document hosting the JS making the request).

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205