Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the cookie values in a backbone.js model?

I have a simple Backbone.js/Bootstrap front end in HTML5 with a Node.js/Restify backend. I am setting cookies in a header response from the server as below:

res.setHeader("Set-Cookie", ["token=ninja", "language=javascript"]);

On the client side, I am making a REST call as

var response = this.model.fetch().success(function(data){
           //success
           }).error(function(data){
           //error 
           }).complete(function(data){
            //complete
           });

that callsback a parse method in the model.

How can I read the cookie value in the model?

like image 492
Ram Iyer Avatar asked Nov 22 '25 11:11

Ram Iyer


2 Answers

Include Cookie.js.

You can then reference individual cookies like this:

var token = Cookie.get('token')

# token == 'ninja'
like image 129
Marty Cortez Avatar answered Nov 24 '25 04:11

Marty Cortez


Here is what I figured out. My application has two components - the HTML/js from one domain that talks to a REST sevice on another domain (and therefore is cross-domain.) Because the cookie is set from REST, it appears is not readable across domains. So the web page will not store the cookie even though the server is sending it. One alternative is to use local cookies or use the technique illustrated by http://backbonetutorials.com/cross-domain-sessions/.

like image 32
Ram Iyer Avatar answered Nov 24 '25 05:11

Ram Iyer



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!