2

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 javascript, 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?

colinmarc
  • 21
  • 2

2 Answers2

1

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.

Sean Vieira
  • 140,251
  • 31
  • 286
  • 277
0

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?

Thomas Schultz
  • 2,284
  • 3
  • 24
  • 34
  • Once a product is picked, then the site redirects to a customization page. It's easy to dynamically generate that page with cheetah, but how does the javascript in that page know which product was picked (or whatever other data it needs)? Then, once the product is customized, should I: a) make an ajax call to an addProduct() method exposed on the cherrypy server when the "finished" link is clicked on the customization page? b) link to an exposed method in a form on the customization page which calls addProduct() and then redirects to the shopping cart? c) something else? – colinmarc May 13 '10 at 21:30