Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Does $resource requests send cookies automatically?

I am using a $resource in my angularJS app. Does it send automatically my cookies? I am doing requests on the same domain.

like image 761
poiuytrez Avatar asked Jul 09 '26 07:07

poiuytrez


2 Answers

Browser will always send a cookie along with the request (no matter if it's an XHR request or not) as long as all assumptions are met (same domain, matching path, matching port, same protocol, not expired, etc.).

Since $resource service is just a simple Ajax wrapper your cookies will/should be sent (if everything's in place).

like image 128
Stewie Avatar answered Jul 12 '26 06:07

Stewie


No. But if you want to send cookies, then you can try $cookies service to get the cookie and send with API either in the payload or included in the header.

You can also set the cookie in a default header (with $cookies service injected) so you don't have to specify it in all API calls.

var cookie = $cookies.myCookie; // suppose you already set $cookies.myCookie= 'xxx';
$http.defaults.headers.post.Cookies = cookie;
like image 38
zs2020 Avatar answered Jul 12 '26 04:07

zs2020



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!