日期:2014-05-19  浏览次数:20851 次

请问js读取HttpCookie写的cookie
我用
HttpCookie   cookie   =   new   HttpCookie( "RoleCookie ");
cookie[ "UserRole "]   =   UserRole;
cookie[ "UserNumber "]   =   UserNumber;
cookie.Expires   =   DateTime.Now.AddDays(1);
HttpContext.Current.Response.Cookies.Add(   cookie   );
写的cookie
想用js读出来
在网上找到的方法是
function   setCookie(name,value)
{
        var   Days   =   30;
        var   exp     =   new   Date();         //new   Date( "December   31,   9998 ");
                exp.setTime(exp.getTime()   +   Days*24*60*60*1000);
                document.cookie   =   name   +   "= "+   escape   (value)   +   ";expires= "   +   exp.toGMTString();
}

function   getCookie(name)
{
        var   arr,reg=new   RegExp( "(^|   ) "+name+ "=([^;]*)(;|$) ");
                if(arr=document.cookie.match(reg))   return   unescape(arr[2]);
                else   return   null;
}
我用getCookie读不到我写的cookie
但是要是用setCookie()这个方法写的cookie就可以用getCookie()方法读到
这是为什么啊

这是我写的cookie
RoleCookie
UserRole=1&UserNumber=testName
localhost/
1024
2748403328
29847982
2102759824
29847781
*



------解决方案--------------------
你直接
alert(document.cookie )看有无东西先~
------解决方案--------------------
其实它就是把cookie写在一起的,然后加了一些标识符号来分隔.

<htmL>
<body>
<script>
document.cookie = "123 ";
alert(document.cookie );
</script>
</body>
</html>

具体如何分隔,你自己处理也是可以的.
------解决方案--------------------
//寫COOKIE
function SetCookie(sName, sValue){
document.cookie = sName + "= " + escape(sValue) + "; ";
}
//讀COOKIE
function GetCookie(sName){
var aCookie = document.cookie.split( "; ");
for (var i=0; i < aCookie.length; i++)
{
var aCrumb = aCookie[i].split( "= ");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}
}
function getCookies(name)
{
var arr = document.cookie.match(new RegExp( "(^| ) "+name+ "=([^;]*)(;|$) "));
if(arr != null) return unescape(arr[2]); return null;

}
------解决方案--------------------
关注..
------解决方案--------------------
<script type= "text/javascript ">
// 写 cookie
function setCookie(sName, sValue, iTime)
{
var date = new Date();
date.setTime(date.getTime()+iTime*1000);
document.cookie = escape(sName) + "= " + escape(sValue) + "; expires= " + date.toGMTString();
}

// 读 cookie
function getCookie(sName)
{
var aCookie = document.cookie.split( "; ");
for (var i=0; i < aCookie.length; i++)
{
var aCrumb = aCookie[i].split( "= ");