用asp.net创建cookie,用javascript读不出cookie的值,求帮助!
asp.net cs代码:
***************************************************************************************
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
HttpCookie myCookie = Request.Cookies["User"];
if (myCookie == null)
{
myCookie = new HttpCookie("User");
myCookie.Value = "测试用户";
Request.Cookies.Add(myCookie);
}
Label1.Text = "<script>show();</script>";
}
}
*****************************************************************************************
javascript代码:
function getCookieValue(cookieName)
{
var cookies=document.cookie;
var cookieIndex=cookies.indexOf(cookieName);
if(cookieIndex!=-1)
{
var cookieValueStart=cookieIndex+cookieName.length+1;
var cookieValueEnd=cookies.indexOf(";",cookieValueStart);
if(cookieValueEnd==-1)
{
cookieValueEnd=cookies.length;
}
var cookieValue=cookies.substring(cookieValueStart,cookieValueEnd);
}
return cookieValue;
}
function show()
{
window.alert("Usercookie的值为:"+getCookieValue("User")+"\n");
}
*******************************************************************************************
输出结果是:Usercookie的值为:undefined
应该是编码问题,但是我在网上找了一些修改方案,还是解决不了,所以还是把原来的代码弄上来,希望大家可以帮我解决这个问题!!