admin管理员组

文章数量:1327945

I set a cookie on Rails like this,

cookies[:account] = { :value => @account.to_s, :expires => 3.month.from_now }

Which seems to be working fine, a simple debug @account shows

--- myvalue
…

But when calling the cookie using jQuery.Cookie It return a "[object Object]" instead.

> $.cookie('account');
"[object Object]"

Any idea why is this happening and how to solve it?

I set a cookie on Rails like this,

cookies[:account] = { :value => @account.to_s, :expires => 3.month.from_now }

Which seems to be working fine, a simple debug @account shows

--- myvalue
…

But when calling the cookie using jQuery.Cookie It return a "[object Object]" instead.

> $.cookie('account');
"[object Object]"

Any idea why is this happening and how to solve it?

Share Improve this question asked Nov 15, 2012 at 22:28 MartinMartin 11.3k23 gold badges86 silver badges143 bronze badges 2
  • Do you change any of jQuery Cookie's parameters? – Julien Royer Commented Nov 15, 2012 at 22:37
  • No. Haven't changed anything. – Martin Commented Nov 15, 2012 at 22:38
Add a ment  | 

5 Answers 5

Reset to default 3

I know I am pretty late to the answers here, but just thought I would post my solution on this too. I battled for hours with this issue of jQuery Cookie returning [object Object], and found some possible cases that would cause this:-

  • Same cookie name but at a different path
  • I found passing the parameters for path/expiry, that the plugin was sometimes treating this as if I were setting a value, and it was trying to set a JSON-like object with path and expiry as the value.
  • Loading the script twice with the cookie set code running on load
  • AJAX loaded content refreshing the script can cause this issue to occur.

In the end, I decided to use the localStorage option as my data being stored was very small, and not worth the time wasted trying to get jQuery cookie to work.

I hope this helps someone else, and saves them from the hours of frustration that I had!

To anyone else that runs into this, check to make sure you don't have multiple cookies of the same name (with different paths or expirations). This can lead $.cookie() to return [object Object]

[object Object] is the return value from Object.toString(), so that means that $.cookie('account') is returning a non-Number, non-String object.

On way to start figuring out what's in the return value (in an effort to help you determine what's in the object returned) is to loop over the properties to figure it out.

So, like this:

var obj = $.cookie('account');
var msg = [];
for(var i in obj)  msg.push( i +" = " + obj[i]);
alert(msg.join("\n")); // or console.log(msg.join("\n"));

You can use Mozilla Library. You'll be able to set and get cookies like this

docCookies.setItem(name, value);
docCookies.getItem(name);

Cookie has a maximum size of 4kb only. That could be one of the reasons.

As for my case. It certainly exceeds 4kb looking at the data alone.

My alternative though is using localStorage.

本文标签: javascriptWhy jQuerycookie returns quotobject Objectquot for a single string valueStack Overflow