将数据从cherrypy服务器端传递到javascript客户端
大局问题。我有一个 Cherrypy 服务器,运行着用 python 编写的电子商务网站的所有购物车方法。我正在前端使用jquery。
在 javascript 中发布到我的 python 方法很容易,但不能以其他方式传递数据。我可以用 JSON 将其发回,但并不总是方便。看起来最简单的方法就是用 cheetah 创建 JavaScript 变量,比如 var width = $width ,但这看起来很混乱。
我在这里做的根本错误是什么?看来我根本没有正确构建服务器-客户端交互。调用我的服务器方法的最佳方法是什么?将服务器信息嵌入到页面中以便可以使用 JavaScript 进行处理的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能给出的唯一答案是问另一个大问题——你的 JavaScript 真正需要多少数据才能完成它的工作?一些内部数据可能应该存储在会话变量中,因为您的用户不需要/不应该能够查看或更改该数据。客户端需要的此类数据可以通过三种方式传递:
var width = $width
示例)your-domain.com/products?id=27
并让您的脚本查找该变量并执行其需要的操作。)所有这三种方法都是完全合法的——唯一的问题是,你的 JavaScript 需要做多少工作,以及你希望在客户端和服务器端做多少重复的工作?
1
是最简单的,但会鼓励草率的 JavaScript 编码习惯(因为您可以使用服务器端模板语言生成大量代码,而不是重构代码来解决问题。2
可能是最快的,但是当您需要添加更多功能时,它的复杂性会急剧增加 - 从长远来看,它会变得最难维护,除非您事先对自己想要的东西有很好的了解.3
是最好的,但如果不产生安全漏洞或做双重工作,它是最难实现的——然而,一旦完成,您就已经完成了一半以上的工作 API。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:
var width = $width
example)your-domain.com/products?id=27
for example and have your script look for that variable and do what it needs to.)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.据我所知,你在这里所说的可以通过我知道的两种方式来完成。
如果您在较低级别上交谈,您可以在 http 请求中获取有关新连接的客户端的一些信息。
我不太确定你在这里问什么。您能举一个更具体的例子吗?
As far as I know what you're talking about here can be done in two ways that I know.
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?