如何在Google App Engine上使用Python发送JSON格式的Cookie数据? [英] How can I send JSON-formatted cookie data using Python on Google App Engine?
问题描述
我正在尝试使用Python脚本对对象进行编码并将其设置为cookie,以便可以使用客户端JavaScript读取它.
我尝试执行此操作的每种方式都遇到了问题.通常,cookie的格式化方式会导致JSON.parse()中断.
我当前的脚本:
cookie = Cookie.SimpleCookie()
data = {"name": "Janet", "if_nasty": "Ms. Jackson"}
cookie['test'] = json.dumps(data)
self.response.headers.add_header("Set-Cookie", cookie.output(header=''))
...导致
test="{\"name\": \"janet\"\054 \"if_nasty\": \"Ms. Jackson\"}"
在客户端上.
我真的不想引入一种hack-y解决方案来替换逗号实例.有什么想法可以使用Python传递复杂的数据结构(通过设置和读取cookie)?
我还想读取客户端上的cookie(已在服务器上设置).我通过base64编码JSON String解决了这个问题,但是这种方法也涉及一些小问题.
1:Base64字符串以0-2等号结尾,这些正被转换为字符串\ 075.我的方法是在客户端上将这些字符还原为相等的字符.
2:base64字符串在cookie中用双引号引起来.我在客户端上删除了这些.
服务器:
nav_json = json.dumps(nav_data)
nav_b64=base64.b64encode(nav_json)
self.response.set_cookie('nav_data', nav_b64)
客户:
var user_data_base64= $.cookie('nav_data');
// remove quotes from around the string
user_data_base64 = user_data_base64.replace(/"/g,"");
// replace \075 with =
user_data_base64 = user_data_base64.replace(/\\075/g,"=");
var user_data_encoded=$.base64.decode( user_data_base64 );
var user_data = $.parseJSON(user_data_encoded);
我在这里使用2个jquery插件: https://github.com/carlo/jquery-base64 和 https://github.com/carhartl/jquery-cookie
注意:我认为这是一个hack:最好重新实现以javascript编码cookie的python代码,但是这样做还有一个缺点,您需要注意并移植和更改该代码. /p>
我现在转到了一个解决方案,在该解决方案中,我使用一个小的html文件在客户端设置了cookie,然后重定向到所请求的实际页面.这是我正在使用的JINJA2模板的摘录:
<script type="text/javascript">
var nav_data='{% autoescape false %}{{nav_data}}{% endautoescape %}';
$.cookie('nav_data', nav_data, { path: '/' });
window.location.replace("{{next}}")
</script>
注意2:对于我的用例而言,Cookie并不理想,我可能会继续使用Session或Local Storage来减少网络开销(尽管我的nav_data很小-大约12个字符).
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 to do this. Generally, the cookie is formatted in a way that makes JSON.parse() break.
My current script:
cookie = Cookie.SimpleCookie()
data = {"name": "Janet", "if_nasty": "Ms. Jackson"}
cookie['test'] = json.dumps(data)
self.response.headers.add_header("Set-Cookie", cookie.output(header=''))
... which results in
test="{\"name\": \"janet\"\054 \"if_nasty\": \"Ms. Jackson\"}"
on the client.
I don't really want to introduce a hack-y solution to replace instances of commas when they appear. Any ideas how I can pass complex data structures (both by setting and reading cookies) with Python?
I also wanted to read a cookie (that had been set on the server) on the client. I worked around the issue by base64 encoding the JSON String, however there are a few small gotchas involved with this approach as well.
1: Base64 strings end with 0-2 equal signs, and these were being converted into the string \075. My approach is to revert those characters into equal characters on the client.
2: The base64 string is being enclosed in double quote characters in the cookie. I remove these on the client.
Server:
nav_json = json.dumps(nav_data)
nav_b64=base64.b64encode(nav_json)
self.response.set_cookie('nav_data', nav_b64)
Client:
var user_data_base64= $.cookie('nav_data');
// remove quotes from around the string
user_data_base64 = user_data_base64.replace(/"/g,"");
// replace \075 with =
user_data_base64 = user_data_base64.replace(/\\075/g,"=");
var user_data_encoded=$.base64.decode( user_data_base64 );
var user_data = $.parseJSON(user_data_encoded);
I am using 2 jquery plugins here: https://github.com/carlo/jquery-base64 and https://github.com/carhartl/jquery-cookie
Note: I consider this a hack: It would be better to re-implement the python code that encodes the cookie in javascript, however this also has the downside that you would need to notice and port and changes to that code.
I have now moved to a solution where I use a small html file to set the cookie on the client side and then redirect to the actual page requested. Here is a snippet from the JINJA2 template that I am using:
<script type="text/javascript">
var nav_data='{% autoescape false %}{{nav_data}}{% endautoescape %}';
$.cookie('nav_data', nav_data, { path: '/' });
window.location.replace("{{next}}")
</script>
Note 2: Cookies are not ideal for my use case and I will probably move on to Session or Local Storage to reduce network overhead (although my nav_data is quite small - a dozen characters or so.)
这篇关于如何在Google App Engine上使用Python发送JSON格式的Cookie数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!