Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If there is a set-cookie response from an xmlhttprequest, will the browser honor it and set the cookie?

I'm not trying to send cookie data with the xmlhttprequest, rather I'm trying to use the xmlhttprequest to set cookies for the session without requiring user interaction.

like image 629
stu Avatar asked Jul 23 '09 20:07

stu


People also ask

Can XMLHttpRequest set cookie?

The default is false . XMLHttpRequest responses from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request.

What does set cookie from response header do?

The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.

Do cookies can be set by server in response?

A regular cookie can be set server side or client side. The 'classic' cookie will be sent back with each request. A cookie that is set by the server, will be sent to the client in a response. The server only sends the cookie when it is explicitly set or changed, while the client sends the cookie on each request.

Can you set cookie in GET request?

Also I would say it is perfectly acceptable to change or set a cookie in response for the GET request because you just return some data.


1 Answers

According to the spec:

If the user agent supports HTTP State Management it should persist, discard and send cookies (as received in the Set-Cookie and Set-Cookie2 response headers, and sent in the Cookie header) as applicable.

As far as I've managed to tell, Firefox 3.6 will store the cookies, but they won't be available to the document of the page which executed the query through document.cookie. But they will be sent with subsequent XMXHttpRequests, which is sufficient for my uses.

like image 62
Douglas Avatar answered Sep 20 '22 17:09

Douglas