Martin Martin - 2 years ago 100
Javascript Question

Why jQuery.cookie returns "[object Object]" for a single string value

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

--- myvalue

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

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

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

mrk mrk
Answer Source

[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"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download