How to get Expess object cookie in angularJS?

前端 未结 1 1886
死守一世寂寞
死守一世寂寞 2020-12-12 04:49

Express documentation says that we can set a cookie with an object using this function:

res.cookie(\'cart\', { items: [1,2,3] });

I want to

相关标签:
1条回答
  • 2020-12-12 05:25

    You can make JSON-string manually:

    res.cookie('cart', JSON.stringify({ items: [1,2,3] }) );
    

    Prefix j: added in ./express/lib/response.js (lines 787-789):

    var val = typeof value === 'object'
        ? 'j:' + JSON.stringify(value)
        : String(value);
    

    where value is your object { items: [1,2,3] }

    It's not part of any standard, as the RFC for cookies says the value can only be a string. Ideally if we followed the standard, we would reject your cookie if it wasn't a string. As a convenience, Express.js allows you to set non-strings as the values, and we'll JSON.stringify the value, pre-pending a j: so we know the value should be JSON.parsed when we read it again for you.

    https://github.com/expressjs/express/issues/2815

    0 讨论(0)
提交回复
热议问题