简体   繁体   中英

How to delete a cookie using coldfusion which is created through javascript or the viceversa?

I am creating a cookie using javascript by using the code

document.cookie="USERNAME=xyz";

After that I am trying to delete same cookie using coldfusion by the code

<cfcookie name="USERNAME" value="0" expires="now"/>

But I am unable to delete the cookie, it still shows in browsers console(firebug). On dump the cookies, it shows the cookie that is created by javascript even after refreshing the page multiple times.

Same thing happens if a cookie is created by coldfusion and trying to delete it through javascript. Using coldfusion i am able to get the cookie value or change the cookie value of the cookie that is created by javascript but unable to delete that cookie.

Using this code to change/set the value of cookie using coldfusion

<cfset coookie.USERNAME = 0> 

Is there any reason for this or any way to delete?

I was just wrestling with this myself. For me, it was a preservecase issue. By default, when setting cookies with the <cfcookie> tag, ColdFusion sets the cookie as an all caps entry. Therefore, when you delete it with <cfcookie> it deletes as expected since the delete is also looking for the cookie with all caps.

However , when setting the cookie with JavaScript, in all likelihood you were setting the cookie with a lowercase (or mixed-case name), so when CF went to delete the cookie, it couldn't find the all caps version.

When you go to delete the cookie, be sure to use the preservecase=true attribute on the <cfcookie> tag.

EDIT: after reading your question again, I am noticing that your casing seems to be correct. However, that is the thing you would definitely want to double check (from my experience), along with the other cookie attributes (path, domain, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM