日期:2014-05-18  浏览次数:20440 次

问个javascript读asp.net写的cookie的问题
 
写cookie是这么写的
  System.Web.HttpCookie cookie = new System.Web.HttpCookie("LoveA");
  cookie.Values["userid"] = userAuth.UserID;
  cookie.Values["username"] = userAuth.UserName;
  cookie.Values["userType"] = userAuth.UserType;

   

  cookie.Expires = DateTime.Now.AddDays(30);
  System.Web.HttpContext.Current.Request.Cookies.Remove(CookieName);
  System.Web.HttpContext.Current.Request.Cookies.Add(cookie);
  System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

怎么用javascript读出来呢?谢谢了

------解决方案--------------------
其实MSDN和网上一大把,搜索一下就行了。
JScript code

//写入COOKIE
var oTime=new Date();
var sValue="竹子"
document.cookie="userid="+sValue+";expires="+(oTime+9999);

//读取

//-----------------------
//偶直接COPY MSDN上的给你了,要打的话好长
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

------解决方案--------------------
那就用JS读JS写不就可以了。

不过也没有关系,他们是可以互相操作的。