将数据从cherrypy服务器端传递到javascript客户端

发布于 2024-09-01 07:32:00 字数 327 浏览 8 评论 0原文

大局问题。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-09-08 07:32:00

我能给出的唯一答案是问另一个大问题——你的 JavaScript 真正需要多少数据才能完成它的工作?一些内部数据可能应该存储在会话变量中,因为您的用户不需要/不应该能够查看或更改该数据。客户端需要的此类数据可以通过三种方式传递:

  1. 内置到服务器端的模板中(您的 var width = $width 示例)
  2. 从查询中提取- 字符串参数或 URL 片段(例如重定向到 your-domain.com/products?id=27 并让您的脚本查找该变量并执行其需要的操作。)
  3. 让您的脚本执行 ajax调用服务器并让服务器传回它需要的数据。

所有这三种方法都是完全合法的——唯一的问题是,你的 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:

  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.

廻憶裏菂餘溫 2024-09-08 07:32:00

据我所知,你在这里所说的可以通过我知道的两种方式来完成。

  1. AJAX 请求可以返回您想要的任何内容。
  2. 多次发布到页面和更改页面(视图)的逻辑

如果您在较低级别上交谈,您可以在 http 请求中获取有关新连接的客户端的一些信息。

我不太确定你在这里问什么。您能举一个更具体的例子吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文