Passing data from cherry server side to client side javascript
Great question with a picture. I have a cherry-pick server working with all shopping cart methods for my ecommerce site written in python. I am working with jquery on the front.
POST for my python methods is easy in javascript, but doesn't pass data in any other way. I can send it back with JSON, but not always convenient. It seems like the easiest way is to just create javascript variables with cheetah type var width = $width
, but it seems like a mess.
What am I doing mostly wrong here? It doesn't seem like I'm structuring the server and client interactions correctly. What's the best way to call my server methods, and what's the best way to embed information from the server into the page so that javascript can work?
a source to share
The only answer I can give is to ask another big question - how much data in your JavaScript really needs to be done? Some internal data should probably be stored in session variables, as your users should not / should not view or modify this data. Such data, which is required on the client side, can be transferred in three ways:
- Server side inline template (example
var width = $width
) - Pushed from query string parameters or URL fragments (e.g. redirects to
your-domain.com/products?id=27
and your script looks for that variable and does what it needs.) - Have the script make ajax calls on the server and pass the data back to the server.
All three methods are perfectly legal - the only question is how much work does your JavaScript do, and how much duplicate work do you want to do on the client and server side?
1
easiest, but can encourage sloppy JavaScript coding habits (since you can use a server-side programming language to generate boxes of code rather than refactoring code to fix the problem. 2
probably the fastest, but its complexity grows astronomically as you need add additional features - and it will be very difficult to maintain in the long run unless you have a very good vision of what you want in
3
advance.is the best, but it is most difficult to implement without creating security holes or doing double work - once it is done, however you are more than halfway to a working API.
a source to share
As far as I know, what you are talking about here can be done in two ways that I know.
- AJAX requests can return whatever you need.
- Multiple posts in pages and logic that change pages (views)
If you are speaking at a lower level, you can get some information in the HTTP request about the recently connected client.
I'm not really sure what you are asking here. Could you provide a more specific example?
a source to share