无法读取Cookie值 [英] Unable to read cookie value
问题描述
无法从cookie中读取cookie值.我已经在脚本方面从javascript设置了cookie值,并尝试从编码部分读取cookie值,即. default.aspx.vb.我在下面显示了我的JavaScript页面:
am unable to read a cookie value from the cookies. i have set cookie value from javascript in scripting side and try to read cookie value from coding part ie. default.aspx.vb. i have shown my javascript page below:
function setcookie()
{
var today=new Date();
var expire=new Date();
var ndays;
if(ndays==null || ndays==0)ndays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = "hostname"+"="+VIH_HostName+ ";expires="+expire.toGMTString();
alert(VIH_HostName)
}
此函数从body标签中的onload调用.
这是我的vb代码,用于读取cookie值
this function called from onload in body tag.
here is my vb code to read cookie value
If Not Request.Cookies("hostname") Is Nothing Then
count = Request.Cookies("hostname").Value
Response.Write("cookie: " & count)
End If
如果我读了cookie,我只会得到空字符串.任何人都可以帮助我解决此问题..
问候
Sasikumar
if i read the cookie i get only null string.. anyone help me to resolve this problem..
regards
Sasikumar
推荐答案
您的JavaScript错误:nDays而不是ndays且未定义变量VIH_HostName.
我更改了js代码.
试试这个:
Your javascript has errors: nDays instead of ndays and variable VIH_HostName is not defined.
I changed the js code.
Try this:
<script type="text/javascript">
function setcookie() {
var today = new Date();
var expire = new Date();
var ndays;
if (ndays == null || ndays == 0) ndays = 1;
expire.setTime(today.getTime() + 3600000 * 24 * ndays);
document.cookie = "hostname=VIH_HostName;expires=" + expire.toGMTString();
}</script>
然后像这样从服务器端获取Cookie
and get the cookie from server side like this
Response.Write(Request.Cookies["hostname"].Value);
它适用于我
It works for me
如果您使用JavaScript设置Cookie并通过VB.net读取它,则名称可能有问题.
您可以遍历请求对象的cookie集合,然后使用调试器逐步浏览-看看您是否完全识别出cookie.
there may be an issue with the name if you''re setting a cookie using javascript and reading it with VB.net.
you could loop through the cookies collection of the request object and step through it using the debugger - see if you recognise your cookie at all.
这篇关于无法读取Cookie值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!