Javascript-从Cookie中检索数据作为“值";不是“原始数据" [英] Javascript - retrieving data from a cookie as "value" not "raw data"

查看:88
本文介绍了Javascript-从Cookie中检索数据作为“值";不是“原始数据"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此建议的功能"处理从"Wordpress Favourites"插件创建的cookie,当从cookie中检索值时.

Working with cookie created by "Wordpress Favourites" plugin, when retrieving the value from the cookie, using this suggested function.

    // Read Cookie For Favorites Functionality - simplefavourites
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

我们重新获得原始数据" ...这是指您在进行调试时看到的文本-> cookie->选择cookie->切换到原始数据"视图.

We get the "RAW DATA" back... by this I mean the text you see when going to firebug -> cookies -> select cookie -> switch to "raw data" view.

它看起来像这样:

%5B%7B%22site_id%22%3A1%2C%22posts%22%3A%5B17411%2C22578%5D%2C%22groups%22%3A%5B%7B%22group_id%22%3A1%2C%22site_id%22%3A1%2C%22group_name%22%3A%22Default+List%22%2C%22posts%22%3A%5B17411%2C22578%5D%7D%5D%7D%5D

当我们真正想要的是值"中的文本时,它看起来像这样,用于jQuery接口.

When what we actually want is the text from "value" which looks like this, for use in interface jQuery.

[{"site_id":1,"posts":[17411,22578],"groups":[{"group_id":1,"site_id":1,"group_name":"Default List","posts":[17411,22578]}]}]

我了解这两者之间的唯一区别是字符转换,但我们确实希望避免每次该函数运行以从Cookie中检索一个简单的值数组时字符转换的开销.

I understand the only difference between these is character translation, but we really want to avoid the overhead of character translation every time this function runs just to retrieve a simple array of values from cookie.

反正有将cookie内容读取为值"而不是原始数据"吗?

Is there anyway of reading the cookie contents as the "value" rather then "raw data"?

在此先感谢您的反馈.

------------建议的解决方案------------------- 使用@Louys Patrice Bessette的反馈意见

------------PROPOSED SOLUTION--------------- working with feedback from @Louys Patrice Bessette

    // Read Cookie For Favorites Functionality - simplefavourites
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    var cookiecontents = readCookie('simplefavorites');
    cookiecontents = decodeURI(cookiecontents); 
    alert(cookiecontents);

这将cookie值返回为: [{"site_id"%3A1%2C"posts"%3A[17411%2C22578]%2C"groups"%3A[{"group_id"%3A1%2C"site_id"%3A1%2C"group_name"%3A"Default+List"%2C"posts"%3A[17411%2C22578]}]}]

This returns the cookie value as: [{"site_id"%3A1%2C"posts"%3A[17411%2C22578]%2C"groups"%3A[{"group_id"%3A1%2C"site_id"%3A1%2C"group_name"%3A"Default+List"%2C"posts"%3A[17411%2C22578]}]}]

其中清除了部分字符转换,但不是全部,看起来像:"和,"没有被解码"

Which clears up some of the character translation but not all, looks like ":" and "," are not getting "decoded"

------------解决的问题----------------- 使用@Louys Patrice Bessette的反馈意见

------------PROBLEM SOLVED--------------- working with feedback from @Louys Patrice Bessette

    // Read Cookie For Favorites Functionality - simplefavourites
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    var cookiecontents = readCookie('simplefavorites');
    cookiecontents = decodeURI(cookiecontents.replace(/%3A/g, ":").replace(/%2C/g, ",").replace(/\+/g, "%20"));
    alert(cookiecontents);

推荐答案

不是原始数据",而是URL编码的字符串.

That ain't "raw data", that is URL encoded string.

如果希望结果为对象,数组或包含URL不允许的字符的字符串,请对readCookie()返回的值使用decodeURI().

Use decodeURI() on the value returned by readCookie() if you expect the result to be an object, array or a string containing characters not allowed in an URL.

请参见MDN 文档 W3C学校示例.


编辑

好像您的cookie是奇怪地编码的...

Seems like your cookie was strangely encoded...

一些不需要编码的字符是...
不需要的.

Some characters not needed to be encoded were...
And some needed weren't.

在您的字符串中,我找到了3.

In your string, I found 3.

%3A ==> :
%2C ==> ,
+ ==> %20

因此,正确解码Cookie的方法是:

So the way to correctly decode your cookie would be:

var correctlyDecoded = decodeURI(cookieVal.replace(/%3A/g, ":").replace(/%2C/g, ",").replace(/\+/g, "%20"));

您应该仔细查看cookie的编码方式...因为其他cookie中可能还会出现更多错误字符.

You should have a close look at how your cookie gets encoded... Because it may happen to have some more characters in fault in other cookies.

您将可以使用 此CodePen 找到那些.

You will be able to use this CodePen to find those.

这篇关于Javascript-从Cookie中检索数据作为“值";不是“原始数据"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆