为什么jQuery.cookie返回"[object Object]"?对于单个字符串值 [英] Why jQuery.cookie returns "[object Object]" for a single string value
问题描述
我像这样在Rails上设置cookie,
I set a cookie on Rails like this,
cookies[:account] = { :value => @account.to_s, :expires => 3.month.from_now }
看起来不错,显示一个简单的debug @account
Which seems to be working fine, a simple debug @account
shows
--- myvalue
…
但是当使用jQuery.Cookie调用cookie时,它将返回"[object Object]"
.
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?
推荐答案
[object Object]
是Object.toString()
的返回值,这意味着$ .cookie('account')返回的是非Number,非字符串对象.
[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.
所以,像这样:
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"));
这篇关于为什么jQuery.cookie返回"[object Object]"?对于单个字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!