How can I send JSON-formatted cookie data using Python on Google App Engine?

前端 未结 3 739
暖寄归人
暖寄归人 2021-01-12 09:47

I\'m trying to encode an object in a Python script and set it as a cookie so I can read it with client-side JavaScript.

I\'ve run into problems every way I\'ve tried

3条回答
  •  温柔的废话
    2021-01-12 10:30

    On the Python side:

    1. json.dumps the string
    2. escape spaces - just call .replace(' ', '%20')
    3. Call urllib.parse.quote_plus() then write the string to the cookie

    On the JavaScript side:

    1. read the cookie
    2. pass it through decodeURIComponent()
    3. JSON.parse it

    This seems to be the cleanest way I've found.

提交回复
热议问题