English 中文(简体)
我如何通过谷歌应用发动机发送JSON-formatted cookie数据?
原标题:How can I send JSON-formatted cookie data using Python on Google App Engine?

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.

我每次都试图这样做,都会遇到问题。 一般说来, co的形式使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"54 "if_nasty": "Ms. Jackson"}"

客户。

我并不真的想引入一种ck折的解决办法,以取代 appear马的事例。 我如何通过复杂的数据结构(通过建立读书库)与“Sharma”进行思考?

最佳回答

我也想读到客户的一台厨师(在服务器上安装)。 我以第64号基调来处理这个问题,但也有少数小道子参与这种做法。

图1:64 显示线以0-2号等同标志告终,这些迹象正被转化成75。 我的做法是将这些特点重新归为客户的同等性质。

2. 第64号基体在 co中被置于双重引号之上。 我删除这些内容。

服务器:

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 75 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.

现在我已转向一种解决办法,即我使用一个小的html文档,把 co子放在客户一边,然后转到所要求的实际网页上。 这里是我使用的JINJA2模板的一个幻灯片:

<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.)

问题回答

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

Java边:

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

这似乎是我发现的最干净的方法。

不能确定 co是这样做的最佳方式? 见,开始向客户提供数据。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签