1

我正在尝试读取名为 Pdf 的 cookie,它具有以下值

http://engine.edocbuilder.com/include/fileDownload-aspx?p=xBfpz3UGPkWamLTDILo%2fWbFqh3FomdYuByiTwfB4RXN0sDN6tY%2fDfxJzzfPZblUl5aSBO3v96%2bJ6acwT7L5oi8tyMuGwKshYtGK%2bfhgiSfM%3d&s=4995b496-e735-4a26-9801-253a34ab0481

但是当我读到这个 ​​cookie 时,我得到了以下格式的值,你能告诉我解决方案吗?

s=a4823a90%2D80e6%2D4006%2D909c%2D69d567f2d318&http%3A%2F%2Fengine%2Eedocbuilder%2Ecom%2Finclude%2FfileDownload%2Easpx%3Fp=%2BRj53ETGUI%2FeK7H8yo2Zj%2F6z9Ggmk4VEgmPEtoA2NPDhomzjaYvk2wkh0OZzWt8OtDcATY%2BknqGG8AuxddA6LWccWAfbQgtI0dlVkWevheg%3D

以下是读取cookie的代码:

var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
     x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
     y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
     x=x.replace(/^\s+|\s+$/g,"");
     if (x==c_name)
     {
         return unescape(y);
     }
 }
4

3 回答 3

1

读取和写入 cookie 使用这个函数:

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString() + "; path=/");
}
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) 
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
于 2012-05-01T12:07:39.607 回答
0

cookie 包含 URL 编码数据。用于unescape解码字符串:

var cookie = "s=a4823a90%2D80e6%2D4006%2D909c%2D69d567f2d318&http%3A%2F%2Fengine%2Eedocbuilder%2Ecom%2Finclude%2FfileDownload%2Easpx%3Fp=%2BRj53ETGUI%2FeK7H8yo2Zj%2F6z9Ggmk4VEgmPEtoA2NPDhomzjaYvk2wkh0OZzWt8OtDcATY%2BknqGG8AuxddA6LWccWAfbQgtI0dlVkWevheg%3D"
// Find starting position of "http" and retrieve the URL from there
var unEscapedURL = unescape(cookie.substring(cookie.indexOf("http"), cookie.length));

这是一个工作小提琴

于 2012-05-01T12:03:12.523 回答
0

只需使用本机decodeURIComponent方法即可获得正确的格式:

return decodeURIComponent(y);

现场测试用例

“问题”出在编写 cookie 的代码中,由于某种原因,它被编码了。

于 2012-05-01T12:03:43.183 回答