Martin Martin - 6 months ago 30
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
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?

mrk mrk
Answer

[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"));