用于发送到 PHP 的最高效的 JavaScript
我正在开发一个将数据存储在 MongoDB 中的分析解决方案。目前我有一个 javascript,它使用 XMLHTTPRequest 对象通过 GET 方法将数据发送到 PHP 文件。
然后 PHP 在 MongoDB 上创建/更新文档。
这是一种有效的做事方式吗?有更好的方法吗?
虽然目前这只是一个副项目,但我想从一开始就尝试使其成为一个可扩展的解决方案。
I'm working on an analytics solution that stores data in a MongoDB. Currently I have a javascript which uses an XMLHTTPRequest object to send data to a PHP file using the GET method.
The PHP in turn then creates/updates a document on the MongoDB.
Is this an efficient way of doing things? Is there a better way to do it?
Whilst it's just a side project at the moment I want to try and make this a scalable solution from the outset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于浏览器来说,GET 的大小受到限制,而 POST 则没有。
PHP 方面并没有太大变化。
A GET is limited in size for the browser, while a POST is not.
And it doesn't change that much in the PHP side.
对于听起来你正在做的事情,你的实现应该没问题。然而恕我直言,我可能会将 XHR 从 get 更改为 post 只是为了防止发送长数据时出现问题。尽管服务器应该能够处理任意长度的 URL(包括查询字符串),但浏览器通常最多只支持大约 2k 个字符。超过这个范围你就会遇到问题。 POST 没有这个限制。
For what it sounds like you are doing, your implementation should be fine. However IMHO, I would probably change the XHR from get to post simply to prevent problems with sending long data. Although servers should be able to handle URLs (including the query string) of any length, browsers generally only support up to about 2k characters. Anything over that and you would have problems. POST doesn't have this limitation.
正如其他人所说,由于浏览器的限制,有理由使用 POST。但我想再达成一项协议。
POST 在 HTTP 方法定义方面更有意义。 GET 应该是安全的,并且不会更改您的系统状态(数据库)。这通常在服务中强制执行,但在 HTML 表单处理中则不然。
来源
附加阅读:
HTTP 规范 - 方法定义
As others have said, there's a reason to use POST because of browsers' limitations. But I'd like to make another agruement.
POST makes more sense in terms of HTTP method definitions. GET is supposed to be safe and make no changes to your system state (database). This is typically enforced in services, but not as much in HTML form processing.
Source
Additional reading:
HTTP Spec - Method Definitions
你建议的应该没问题。
如果您打算在其他站点上使用 javascript(同时将 php 保留在单个域上),您将遇到跨域策略的问题。
What you propose should be fine.
If you are going to use the javascript on other sites (While keeping your php on a single domain), you are going to run into issues with cross domain policies.