开发者

Jquery Cookie Plugin Returns [object Object]

开发者 https://www.devze.com 2023-03-27 20:39 出处:网络
I have an issue with a site i run that when cookies are disabled completely user cannot continue through a booking process.

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 [obj开发者_高级运维ect 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.


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:

if(isNumeric($.cookie("sourceID"))){
sourceid = $.cookie("sourceID");
}else{
sourceid = 27201;
}

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消