开发者

Passing data from the cherrypy server-side to javascript client-side

开发者 https://www.devze.com 2022-12-30 09:30 出处:网络
Big picture question. I\'ve got a cherrypy server running with all the shopping cart methods for my e-commerce site written in python. I\'m working with jquery on the front end.

Big picture question. I've got a cherrypy server running with all the shopping cart methods for my e-commerce site written in python. I'm working with jquery on the front end.

POSTing to my python methods is easy in jav开发者_运维百科ascript, but not passing data the other way. I can send it back with JSON, but not always conveniently. It seems like the easiest way is to just create javascript variables with cheetah like var width = $width but that seems messy.

What am I doing fundamentally wrong here? It doesn't seem like I'm structuring my server-client interaction correctly at all. What's the best way to have my server methods called, and what's the best way to embed information from the server into a page so that it can be worked on with javascript?


The only answer I can give is to ask another big picture question -- how much data does your JavaScript really need to do its job? Some internal data should probably be stored in session variables, since your users will not need to / should not be able to view or alter that data. Such data as is needed on the client side can be passed in three ways:

  1. Built into the template on the server side (your var width = $width example)
  2. Pulled from query-string parameters or URL fragments (redirect to your-domain.com/products?id=27 for example and have your script look for that variable and do what it needs to.)
  3. Have your script make ajax calls to the server and have the server pass back the data it needs.

All three methods are perfectly legitimate -- the only question is, how much work does your JavaScript have to do, and how much do you want to be doing duplicate work on the client and server side?

1 is easiest, but can encourage sloppy JavaScript coding habits (since you can use your server-side templating language to generate reams of code, rather than refactoring the code to fix the problem.
2 is probably quickest, but its complexity grows astronomically as you need to add more features -- and it becomes the most difficult to maintain in the long run unless you have a very good vision of what you want beforehand.
3 is the best, but it is the hardest to implement without creating security holes or doing double work -- once it's done, however, you are more than halfway to a working API.


As far as I know what you're talking about here can be done in two ways that I know.

  1. AJAX requests can return whatever you want.
  2. Multiple posting to pages and logic that changes the pages(views)

If you're talking at a lower level, you can get some information in the http request about your newly connected client.

I'm not really sure what all you're asking here. Could you give a more specific example?

0

精彩评论

暂无评论...
验证码 换一张
取 消