输出缓冲和 AJAX
当用户单击我网站上的按钮时,页面的特定部分将被刷新,但渲染整个部分需要一段时间。是否可以实时显示/输出该部分的部分内容,而不是等待整个部分完成?如果我将其作为 html 内容的 ajax 请求发送,我该怎么做?谢谢
When a user clicks a button on my site, a particular part of a page will be refreshed, but it takes a while to render the entire section. Is it possible to display/output parts of the section in real time rather than wait for the entire section to finish? How would i do this if im sending it as an ajax request for html content? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议在 AJAX 调用中设置
cache: true
(如果使用 jQuery),无论哪种方式,您都需要设置 HTTP 响应标头。 这里是一些示例。 通过设置cache-control
标头和expires
等,您的 AJAX 请求 - 如果未更改 -将从缓存加载。这将大大加快速度。一个简单的例子:
注意:这不适用于 POST 请求,只能使用 GET。
I would recommend setting
cache: true
in your AJAX call (if using jQuery) and either way, you're going to want to set the HTTP response headers. Here are some examples. By setting thecache-control
headers andexpires
etc, your AJAX requests - if unchanged - will be loaded from cache instead. This will drastically speed things up.A quick example:
Note: this will not work with POST requests, just GET.
如前所述,缓存 AJAX 请求是一个不错的选择。除此之外,我认为您可以做的让您的应用程序看起来更快就是向用户显示 进度条使用 AJAX(重新)加载内容时。
As said, caching your AJAX requests is a good option. Besides that, all I think you can to do make your application seem faster is to show users a progressbar while (re)loading content with AJAX.
您可以实施 Pagelets 技术。本质上,您可以通过如下方式完成此操作:
并使用多个不同的 Ajax 调用来加载站点的每个不同部分。然而,这确实有一个缺点,即增加了用户浏览器必须发出的 HTTP 请求量。
这是假设不同的部分可以与页面的其余部分分开生成。如果你的所有内容都需要同时生成,那么这个技术就没有什么用处了。
这篇文章值得一读(名为“BigPipe”的 Facebook 项目):http://www.facebook.com/note_id=389414033919" facebook.com/note.php?note_id=389414033919
You could implement a Pagelets technique. Essentially you could work this in a way, like below:
And have several different Ajax calls loading each different part of your site. This does have a disadvantage of increasing the amount of HTTP requests the user's browser has to make, however.
This is presuming though that different parts can be generated separately from the rest of the page. If all your content needs to be generated at the same time then this technique will be of no use to use.
This is of a good read (Facebook project named "BigPipe"): http://www.facebook.com/note.php?note_id=389414033919