Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a set-cookie header being ignored by browser and the cookies not saved from an Ajax call using fetch?

For some reason my browser (I've tried several) is not setting cookies even though a valid set-cookie response is being returned by the server when the GET call is made via Ajax using the fetch api to make the request

If I make the identical GET call via just putting the URL in the browser, the (identical) set-cookie response headers are respected by the browser and the cookies are saved.

I've inspected the request and response headers via LiveHeaders and the Chrome network inspector and there is no difference.

EDIT: To clarify, this is NOT a problem with ajax sending cookies to the server. It is a problem where the cookies are not saved by the browser at all when a response comes back with valid set-cookies headers (which according to documentation should be respected whether ajax or not).

like image 821
Peter Avatar asked Aug 18 '16 21:08

Peter


People also ask

Can AJAX call set cookie?

Yes, you can set cookie in the AJAX request in the server-side code just as you'd do for a normal request since the server cannot differentiate between a normal request or an AJAX request.

What is the difference between set cookie and cookie header?

The Set-Cookie header is sent by the server in response to an HTTP request, which is used to create a cookie on the user's system. The Cookie header is included by the client application with an HTTP request sent to a server, if there is a cookie that has a matching domain and path.

What do I do with cookie set header?

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.

Why is set cookie forbidden?

Set-Cookie is a forbidden response header name. You cannot read it using browser-side JavaScript. If you need to pass that information to your JavaScript, then you need to have the server use some other mechanism (such as a different header or part of the response body).


1 Answers

After much head banging, I solved this issue by setting the 'credentials' property of the request to 'include'. I was under the impression that this only controlled the sending of cookies to the server on fetch requests, but apparently, at least in the implementation I am using, if not set it also means that cookies will not be saved if they are sent back from the server.

From the spec at https://fetch.spec.whatwg.org/

A request has an associated credentials mode, which is "omit", "same-origin", or "include". Unless stated otherwise, it is "omit".

Request's credentials mode controls the flow of credentials during a fetch. When request's mode is "navigate", its credentials mode is assumed to be "include" and fetch does not currently account for other values. If HTML changes here, this standard will need corresponding changes.

Credentials are HTTP cookies, TLS client certificates, and authentication entries.

like image 190
Peter Avatar answered Oct 11 '22 04:10

Peter