Jquery Cookie 插件返回 [object Object]
我运行的网站存在问题,当 cookie 完全禁用时,用户无法继续完成预订流程。
我使用 Jquery 和 cookie 插件,当 cookie 被禁用时,cookie 似乎总是返回 [object Object]。 检查这一点的代码是:
alert($.cookie("sourceID")); //returns [object Object]
if($.cookie("sourceID") === null || $.cookie("sourceID")=== '[object Object]'){
sourceID = 27201;
}else{
sourceID = $.cookie("sourceID");
}
alert(sourceID); //returns [object Object]
如果用户没有启用cookie,上面的代码会尝试设置默认的sourceID。然后将其(与其他信息一起)传递到另一个构建 XML 的函数并将其传递到服务器进行处理;然而,由于 [object Object] 问题,当传递此参数时,服务器无法找到匹配的 sourceid。
I have an issue with a site i run that when cookies are disabled completely user cannot continue through a booking process.
I use Jquery and the cookie plugin, the cookie appears to always return [object Object] when cookies are disabled.
Code for checking this is:
alert($.cookie("sourceID")); //returns [object Object]
if($.cookie("sourceID") === null || $.cookie("sourceID")=== '[object Object]'){
sourceID = 27201;
}else{
sourceID = $.cookie("sourceID");
}
alert(sourceID); //returns [object Object]
the above is trying to set a default sourceID if the user does not have cookies enabled. This is then passed (along with other information) into another function that builds XML and passes it to a server for handling; however due to the [object Object] issue when this is passed the server cannot find a sourceid that matches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决此问题的方法,因为我试图从 cookie 中检索整数值,因此可以检查返回的 cookie 值是否为数字。为此,我使用此问题的答案:验证 JavaScript 中的十进制数字 - IsNumeric() 并使用:
不是检查 cookie 是否返回有效值的最佳方法,但我建议将来遇到此问题的任何人都根据他们知道 cookie 应包含的内容(int、特定字符串等)进行验证
i found a work around for this issue, as i was trying to retrieve an integer value from the cookie it was possible to check to see if the value of the cookie returned was numeric. To do this i use the answer from this question: Validate decimal numbers in JavaScript - IsNumeric() and used:
not the best way to check that the cookie is returning a valid value but i would suggest anyone that hits this issue in the future to validate against what they know the cookie should contain (int, specific string etc)