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

后端 未结 5 1473
故里飘歌
故里飘歌 2021-01-26 11:40

I set a cookie on Rails like this,

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

Which seems to be workin

5条回答
  •  情话喂你
    2021-01-26 11:50

    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!

提交回复
热议问题