用于发送到 PHP 的最高效的 JavaScript

发布于 2024-11-17 00:03:15 字数 212 浏览 3 评论 0原文

我正在开发一个将数据存储在 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 技术交流群。

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

发布评论

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

评论(4

豆芽 2024-11-24 00:03:16

对于浏览器来说,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.

乄_柒ぐ汐 2024-11-24 00:03:16

对于听起来你正在做的事情,你的实现应该没问题。然而恕我直言,我可能会将 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.

暖伴 2024-11-24 00:03:16

正如其他人所说,由于浏览器的限制,有理由使用 POST。但我想再达成一项协议。

POST 在 HTTP 方法定义方面更有意义。 GET 应该是安全的,并且不会更改您的系统状态(数据库)。这通常在服务中强制执行,但在 HTML 表单处理中则不然。

某些方法(例如 HEAD、GET、OPTIONS 和 TRACE)被定义为安全的,这意味着它们仅用于信息检索,不应更改服务器的状态。换句话说,除了相对无害的影响(例如日志记录、缓存、横幅广告服务或增加网络计数器)之外,它们不应该有副作用。因此,在不考虑应用程序状态上下文的情况下发出任意 GET 请求应该被认为是安全的。

来源

附加阅读:

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.

Some methods (for example, HEAD, GET, OPTIONS and TRACE) are defined as safe, which means they are intended only for information retrieval and should not change the state of the server. In other words, they should not have side effects, beyond relatively harmless effects such as logging, caching, the serving of banner advertisements or incrementing a web counter. Making arbitrary GET requests without regard to the context of the application's state should therefore be considered safe.

Source

Additional reading:

HTTP Spec - Method Definitions

雨轻弹 2024-11-24 00:03:16

你建议的应该没问题。

如果您打算在其他站点上使用 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.

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