如何使用ajax和php将100个对象保存到服务器?

发布于 2024-10-16 19:15:49 字数 939 浏览 5 评论 0原文

假设我有一个包含 100 个对象的页面,转换为 json 后每个页面约为 700 字节。

为了将对象保存到基于 php 的控制器,我有以下选项。

选项 1

对于每个对象(100 个对象)执行以下

  1. 获取对象定义 2 .convert to json
  2. 将http post发送到php控制器,
  3. php控制器将其保存到文件或数据库中。

选项2

变量bigJsonString;

对于每个对象(100 个对象)执行以下

  1. 获取对象定义 2 .convert to json
  2. 将 json 附加到字符串变量“bigJsonString”,并使用分隔符来指示对象结束。

在构造完大胖bigJsonString之后,

  1. 通过发送“bigJsonString”将http post发送到php控制器,
  2. php控制器将其保存到文件或数据库中。

在选项 1 中,我接连发布 100 个 http 帖子。这会引起任何警觉吗? 这对于执行 ajax post 的 Web 应用程序来说正常吗?

第二个选项看起来很安全,但唯一的问题是当 100 个对象变成 500 个对象时,或者达到“bigJsonString”达到几兆字节长的程度。

我们可以引入的第三个选项是选项 1 和 2 的混合,我们首先构建“bigJsonString”,如果长度达到一定限制,则执行 ajax post。刷新字符串并为剩余对象再次构建字符串。

有哪些陷阱以及正常或标准做法是什么。如果有人可以指出已经对此进行了分析的资源,那就太好了。

非常感谢。

Let say I have a page with 100 objects and each page is around 700 bytes when converted to json.

In order to save the objects to the php based controller I have the following options.

Option 1

For each objects (100 objects) do the following

  1. Take object definition
    2 .convert to json
  2. Do http post to the php controller
  3. the php controller saves it to a file or database.

Option 2

Variable bigJsonString;

For each objects (100 objects) do the following

  1. Take object definition
    2 .convert to json
  2. append the json to a string variable "bigJsonString" with a delimitter to indicate end of object.

After the big fat bigJsonString is constructed

  1. Do http post to the php controller by sending "bigJsonString"
  2. the php controller saves it to a file or database.

In option 1, I am doing 100 http posts one after another. Does this raise any alarms?
Is this normal for web applications doing ajax post?

The second option seems safe but then the only concern is when the 100 objects become say 500 objects or to a point where the "bigJsonString" goes several Megabytes long.

The third option we can introduce is a hybrid of option 1 and 2 where we start by constructing the "bigJsonString" and if the length goes to a certain limit then do a ajax post. Flush the string and build the string again for remaining objects.

What are the pitfalls and what is the normal or standard practice. if someone can point to resources where this is already analysed, that would be great.

Thanks very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

你是年少的欢喜 2024-10-23 19:15:49

浏览器通常将单个域的连接数量限制为数量较少(大多数浏览器默认低于 20)。与此同时,您的许多请求将被阻止。

另一方面,较大的请求将需要更长的时间才能完全处理,因为并行化的机会较少。

理想情况下,您可以尝试两种方法,看看哪一种最有效。

注意:对于第二种方法,您可以创建对象的数组,然后将数组序列化为JSON,而不是手动处理JSON字符串。 (JSON 支持数组,而不仅仅是对象!)

Browsers generally limit the number of connections to a single domain to a low number (under 20 by default for most browsers). In the meantime, many of your requests will block.

On the other hand, larger requests will take longer to fully process because there are less opportunities for parallelization.

Ideally, you would try both methods and see which one works most effectively.

A note: for the second method, you could create an array of the objects then serialize the array as JSON, instead of manually dealing with the JSON string. (JSON supports arrays, not just objects!)

你另情深 2024-10-23 19:15:49

我猜这是情境性的。但是,我不认为一次性向服务器发送 100 个请求(本身)是好的。

就我个人而言,我只是将每个对象推送到 JavaScript 数组,并将该数组的 JSON 表示形式发送到 PHP,这样您就不必担心分隔符。

I guess its situational. However, I don't see any situation in which sending 100 requests to the server all at one time (per se) is good.

Personally, I'd just push each object to a javascript array and send a JSON representation of the array to PHP, that way you don't have to worry about delimiters.

南城追梦 2024-10-23 19:15:49

这取决于您将对象发布到服务器的速度。如果每秒发布 json 对象,那么每个帖子 1 个对象也不算太糟糕。但如果您在一秒钟内发布 100 个帖子,那么您确实需要建立一个大请求。

每个请求都会有明显的延迟。就性能而言,构建大型多对象 json 字符串更可取。

如果其中一个对象出现错误怎么办?您需要确保它不会转储停止处理所有其他对象,否则用户将不得不再次上传所有这些数据。

如果您执行多个请求,您可以在客户端提供更好的用户反馈,因为您确切地知道自己在对象队列中的位置。

由你来平衡这一切。

祝你好运。

It depends how fast you are posting the objects to the server. If the json objects are being posted say every second, 1 object per post isn't too bad. But if you are posting 100 in a second, you really need to build up a large request.

There will be significant lag for each request. Building a large multi object json string is preferable in terms of performance.

What if there is an error in one of the objects? You will need to make sure it doesn't dump stop processing of all the other objects or the user will have to upload all that data again.

If you do multiple requests, you can give better user feedback client side since you know exactly where you are in the queue of objects.

It is up to you to balance all this.

Good luck.

波浪屿的海角声 2024-10-23 19:15:49

选择选项 2,bigJsonString。您应该可以毫无困难地传递几兆字节长的消息 - 使用相同的基础设施通过互联网传递更大的 html、图像、样式、脚本和视频文件。

Go for option 2, bigJsonString. You should have no trouble passing messages that are several megabytes long - the same infrastructure is used to pass much larger html, image, style, script and video files over the internets.

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