0

I'm using jquery.cookie v1.4.1 to set a cookie like this

$.cookie('userCookie', $("#user").val());

Where $("#user").val() is returning something like 'username'

Then in an angular app, am trying to retrieve this cookie using

var userCookie = $cookies.get('userCookie');

But is not working, I'm getting:

var userCookie = undefined

I'm using AngularJS v1.4.8 with corresponding ngCookies

Any help would be nice....

3
  • Are cookie path or domain different? Commented Feb 22, 2016 at 13:50
  • Yes path are different! It matters!? Commented Feb 22, 2016 at 13:52
  • it does if path is set in cookie Commented Feb 22, 2016 at 13:54

2 Answers 2

1

Thanks to charlietfl I got it to work.

It happens that I was setting the cookie for a specific path, let say '/domain/somepath' and then I was trying to retrieve it from '/domain/someotherpath', where the cookie was not available.

I fix it specifying the path on the cookie to all my domain like this:

 $.cookie('userCookie',$("#user").val(), { path: '/' });
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you use angular to store your cookies?

$cookies.put('userCookie', $("#user").val());

Btw: its horrible if you use JQuery like this inside your Angular App.

$cookies.get('userCookie');

Your problem maybe: the cookie will be created BEFORE your DOM-Element is even rendered on the page. Try $.cookie('userCookie', 'test123'); , this should work pretty sure (with angular 100%).

How to fix this? First of all, DONT USE JQUERY LIKE THIS IF YOU HAVE ANGULAR!!!

<input ng-model="username"></input>

$cookies.put('userCookie', $scope.username);

6 Comments

I'm not storing the cookie with jquery in my angular app. The cookie is stored by other site in which I don't use angular, I'm just trying to retrieve it in my angular app...
Oh, are you sure it's stored in cookies and the name is correct? Other problems: not injected ngCookies and such things, but you would get some exceptions if this happens.
I'm checking the cookies in chrome and it's correctly stored, and I got no error of injection in the service I'm getting the cookie...
Hmm..other thing I found is this: stackoverflow.com/questions/30512473/…. If not, I really don't know whats going on in your application.
Using $cookies.put('userCookie', 'somename'); effectively sets the cookie and allows me to retrieve it, however I don't have Angular available in the site where I need to store the cookie, does setting the cookie with JQuery won't make it available on angular?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.