简体   繁体   English

服务器端与客户端(AJAX)加载

[英]Server-side vs. Client-side (AJAX) Loading

I was wondering what is considered best practice. 我想知道什么是最佳做法。 Let's say I have a dropdown select widget. 假设我有一个下拉选择小部件。 Should it be preloaded with content when the page is served up from the server or once it is loaded, should an AJAX request be made to retrieve the contents and then populate it? 如果在从服务器提供页面或加载页面时预先加载内容,是否应该发出AJAX请求以检索内容然后填充它?

I kinda like the idea of loading it empty and issuing an AJAX call to retrieve the contents. 我有点想把它加载为空并发出一个AJAX调用来检索内容。 But is that going to result in slower page loading times. 但是这会导致页面加载时间变慢。 Especially if the page has several widgets that need to be loaded with content from the server? 特别是如果页面有几个需要从服务器加载内容的小部件?

Does it matter on the amount of content being loaded? 加载的内容量是否重要?

For the dropdown example, I meant the options in the dropdown. 对于下拉示例,我的意思是下拉列表中的选项。 So if I had a dropdown of employees to choose, from I am referring to that list of employees. 因此,如果我有一个下拉的员工可供选择,我指的是员工名单。 Do I load an empty dropdown and on init of the controller have it retrieve the employees and populate the dropdown? 我是否加载一个空的下拉列表并在控制器的init上检索员工并填充下拉列表?

But then I think about a datagrid of let's say 200 rows of emplyees and some employee data that is stored in a database. 但后来我想到了一个数据网格,让我们说200行的员工和一些存储在数据库中的员工数据。 Do I load the page and when the page loads have a controller whose init function retrieves the dataset of employees and populates and displays the datagrid? 我是否加载页面并在页面加载时有一个控制器,其init函数检索员工的数据集并填充并显示数据网格?

Or when the page is served up from the server does it retrieve the dataset on the server-side where it also creates the datagrid and it gets loaded then. 或者,当从服务器提供页面时,它会在服务器端检索数据集,在数据集中它也会创建数据网格,然后加载它。 This is the programming world that I am used to having done mostly PHP, JSP and ASP stuff in the past. 这是编程世界,我过去常常主要完成PHP,JSP和ASP的工作。 Only using JavaScript for some cool page effects, etc. 仅使用JavaScript进行一些很酷的页面效果等。

But I seem to like the idea of once the page is loaded (or being loaded), make AJAX requests to retrieve the data needed to populate the widgets/content on the current screen. 但我似乎喜欢一旦页面加载(或被加载)的想法,使AJAX请求检索填充当前屏幕上的小部件/内容所需的数据。 I am just concerned that the loading of page might seem clunky or slow since I am now making more requests to the server to paint the page. 我只是担心页面的加载可能看起来很笨或慢,因为我现在正在向服务器发出更多请求来绘制页面。 The initial request for the page, and then a request for each dataset needed to populate a widget. 对页面的初始请求,然后是填充小部件所需的每个数据集的请求。

I think the best answer is "it depends on your framework". 我认为最好的答案是“这取决于你的框架”。 I've seen web frameworks that handle this problem both ways. 我见过Web框架可以通过两种方式处理这个问题。 I do think that, especially when a lot of data is being populated into the DOM, it's preferable from a performance perspective to load as much of the page as possible in the initial HTTP response, and only update as needed via AJAX. 我确实认为,特别是当大量数据被填充到DOM中时,从性能角度来看,最好在初始HTTP响应中加载尽可能多的页面,并且只能通过AJAX根据需要进行更新。 Note that for large data sets, the performance overhead of an additional HTTP request is relatively small compared to the impact of performing substantial DOM manipulation via JavaScript -- DOM manipulation can be extremely slow in some browsers. 请注意,对于大型数据集,与通过JavaScript执行大量DOM操作的影响相比,额外HTTP请求的性能开销相对较小 - 在某些浏览器中,DOM操作可能非常慢。

You might get more detailed answers if you add which framework(s) you're using. 如果添加您正在使用的框架,您可能会得到更详细的答案。

As you point out yourself, loading everything with AJAX is going to make the page slow. 正如您自己指出的那样,使用AJAX加载所有内容会使页面变慢。 In fact, one of the best practices for speeding up web pages is to reduce the number of http requests. 事实上,加速网页的最佳做法之一是减少http请求的数量。

That being said, there are some cases where AJAX makes the page load faster. 话虽如此,在某些情况下,AJAX可以加快页面加载速度。 One example is auto complete for a text box. 一个示例是文本框的自动完成。 If implemented such a feature without using AJAX that would require the web page to load every single possible query in the first load. 如果在不使用AJAX的情况下实现这样的功能,则需要网页在第一次加载时加载每个可能的查询。 The page would take forever to load all that data. 该页面将永远加载所有数据。 But the auto complete would probably seem more response than normal once the page had loaded. 但是一旦页面加载,自动完成可能看起来比正常情况更多。

My advice is to start out with the most simple solution you can think of, and then add AJAX, cache and other optimizing features where needed afterwords. 我的建议是从您能想到的最简单的解决方案开始,然后在需要的后续文章中添加AJAX,缓存和其他优化功能。

It depends of the data you are displaying, if you want quick updates from the database the ajax approach is fine. 这取决于您显示的数据,如果您想从数据库快速更新ajax方法是好的。 But if your data is fixed (or you don't care about updating it once the page is loaded) then I shouldn't go with the ajax approach, is more work (requesting json, parsing it, populating the select or whatever) and you don't gain much benefit. 但是如果您的数据是固定的(或者您不关心在加载页面后更新它),那么我不应该使用ajax方法,更多的工作(请求json,解析它,填充选择或其他)和你没有获得太多的好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM