使用Ajax,在服务器或客户端生成额外的标记是否更好?

时间:2022-01-10 16:11:37

Which is better in AJAX request, Response with ready HTML or response with just data and write HTML using JavaScript, and this JavaScript will use a predefined HTML template to put the coming data inside and show on the page.

这在AJAX请求中更好,使用现成的HTML进行响应或仅使用数据进行响应并使用JavaScript编写HTML,此JavaScript将使用预定义的HTML模板将即将到来的数据放入页面并显示在页面上。

Creating the HTML on the server and send to the page, will decrease the client side JS code, but will increase the response size.

在服务器上创建HTML并发送到页面,将减少客户端JS代码,但会增加响应大小。

Sending the data to the client side will decrease the response size but will increase the JS code.

将数据发送到客户端将减少响应大小,但会增加JS代码。

Which is better and most used?

哪个更好,最常用?

5 个解决方案

#1


5  

I think the right solution is highly context dependent. There may be a right answer for a given situation, but there is no one size fits all answer. Generally, if I'm using a partial view that gets replaced via AJAX, I'll return html. If I'm performing an action on a small part of something, I'll use JSON. I'm probably more likely to use JSON as there are more situations where it fits, but I try to pick the best solution for the problem at hand. I'm using ASP.NET MVC, though. Other frameworks undoubtedly have different characteristic solutions.

我认为正确的解决方案是高度依赖于环境的。对于给定的情况,可能有正确的答案,但没有一种尺寸适合所有答案。通常,如果我使用的是通过AJAX替换的部分视图,我将返回html。如果我在一小部分内容上执行操作,我将使用JSON。我可能更有可能使用JSON,因为有更多适合的情况,但我尝试为手头的问题选择最佳解决方案。不过,我正在使用ASP.NET MVC。其他框架无疑具有不同的特征解决方案。

#2


4  

I've seen both used. In addition to the tradeoffs listed in the OP, I'd add:

我见过两个都用过。除了OP中列出的权衡之外,我还要补充:

  • It is better to send the information as data, not html since you'll then have more options in how you will use it.
  • 最好将信息作为数据发送,而不是html,因为您将有更多选择如何使用它。
  • What is your comfort level with JS?
  • 你对JS的舒适程度如何?
  • You can use multiple UI's (on different pages) and can re-use the data on the page. Eg display the data in a short form and in a long form, but use the same data source. -- letting the client switch between the two on a page without requiring a server trip.
  • 您可以使用多个UI(在不同页面上)并可以重复使用页面上的数据。例如,以简短形式和长格式显示数据,但使用相同的数据源。 - 让客户端在页面上切换两者而无需服务器脱机。
  • A pure JS implementation of the liquid template system is available for client-side templating: http://www.mattmccray.com/archive/2008/12/11/Liquidjs_A_Non-Evaling_Templat
  • 液体模板系统的纯JS实现可用于客户端模板:http://www.mattmccray.com/archive/2008/12/11/Liquidjs_A_Non-Evaling_Templat

Larry

拉里

#3


3  

I'll be extremely pragmatic here:

我在这里会非常务实:

It depends on the amount and the complexity of the new markup.

这取决于新标记的数量和复杂性。

If you need to return a elaborate piece of HTML, it's always better to write it in the server side and returning it as data, it's also easier to maintain. Building complex HTML in the client side usually is cryptic, even with the use of modern js libraries. On the other hand, if your extra markup is small, then you can create it with js. I've never done anything with ASP.NET AJAX, but having a asp.net page, a rails view, or a JSP with only a small fragment like <p class='info'>Row Updated</p> its confusing.

如果你需要返回一个精心设计的HTML,最好将它写在服务器端并将其作为数据返回,它也更容易维护。即使使用现代的js库,在客户端构建复杂的HTML通常也很神秘。另一方面,如果您的额外标记很小,那么您可以使用js创建它。我从来没有对ASP.NET AJAX做过任何事情,但是有一个asp.net页面,一个rails视图,或者只有一个像

Row Updated 这样的小片段的JSP令人困惑。

Let the code speak to you, If you're fighting against javascript code to create markup in the client side, maybe it should go in the server side.

让代码跟你说话,如果你正在反对javascript代码在客户端创建标记,也许它应该放在服务器端。

Lastly: don't worry too much about the size of HMTL vs JSON, and if you do, benchmark the requests to see if the difference isn't negligible.

最后:不要太担心HMTL与JSON的大小,如果你这样做,请对请求进行基准测试,看看差异是否可以忽略不计。

#4


1  

I believe the more common method is to pass the mass of markup (HTML/CSS) via synchronous navigation to site, and keep the AJAX request/response as lean as possible [citation needed].

我相信更常见的方法是通过同步导航将大量标记(HTML / CSS)传递到站点,并使AJAX请求/响应保持尽可能精简[需要引证]。

Admittedly, it is pretty rare to see raw HTML returning as an AJAX response. Usually it will be a JSON response, as that is the easiest to eval() with JS.

不可否认,将原始HTML作为AJAX响应返回是非常罕见的。通常它将是一个JSON响应,因为这是使用JS最简单的eval()。

Since already most of the markup and styling classes are needed anyway, and might be used by "static" content as well, that leaves you the opportunity to keep the AJAX small, neat and to the point.

由于大多数标记和样式类都是必需的,并且也可能被“静态”内容使用,这使您有机会保持AJAX的小巧,整洁和重点。

#5


0  

Usually when updating small parts of screen you can send the JSON back to the client and the client can easily update itself. If you are trying to build a complicated Grid then better to use an UpdatePanel.

通常在更新屏幕的小部分时,您可以将JSON发送回客户端,客户端可以轻松更新自己。如果您正在尝试构建复杂的Grid,那么最好使用UpdatePanel。

#1


5  

I think the right solution is highly context dependent. There may be a right answer for a given situation, but there is no one size fits all answer. Generally, if I'm using a partial view that gets replaced via AJAX, I'll return html. If I'm performing an action on a small part of something, I'll use JSON. I'm probably more likely to use JSON as there are more situations where it fits, but I try to pick the best solution for the problem at hand. I'm using ASP.NET MVC, though. Other frameworks undoubtedly have different characteristic solutions.

我认为正确的解决方案是高度依赖于环境的。对于给定的情况,可能有正确的答案,但没有一种尺寸适合所有答案。通常,如果我使用的是通过AJAX替换的部分视图,我将返回html。如果我在一小部分内容上执行操作,我将使用JSON。我可能更有可能使用JSON,因为有更多适合的情况,但我尝试为手头的问题选择最佳解决方案。不过,我正在使用ASP.NET MVC。其他框架无疑具有不同的特征解决方案。

#2


4  

I've seen both used. In addition to the tradeoffs listed in the OP, I'd add:

我见过两个都用过。除了OP中列出的权衡之外,我还要补充:

  • It is better to send the information as data, not html since you'll then have more options in how you will use it.
  • 最好将信息作为数据发送,而不是html,因为您将有更多选择如何使用它。
  • What is your comfort level with JS?
  • 你对JS的舒适程度如何?
  • You can use multiple UI's (on different pages) and can re-use the data on the page. Eg display the data in a short form and in a long form, but use the same data source. -- letting the client switch between the two on a page without requiring a server trip.
  • 您可以使用多个UI(在不同页面上)并可以重复使用页面上的数据。例如,以简短形式和长格式显示数据,但使用相同的数据源。 - 让客户端在页面上切换两者而无需服务器脱机。
  • A pure JS implementation of the liquid template system is available for client-side templating: http://www.mattmccray.com/archive/2008/12/11/Liquidjs_A_Non-Evaling_Templat
  • 液体模板系统的纯JS实现可用于客户端模板:http://www.mattmccray.com/archive/2008/12/11/Liquidjs_A_Non-Evaling_Templat

Larry

拉里

#3


3  

I'll be extremely pragmatic here:

我在这里会非常务实:

It depends on the amount and the complexity of the new markup.

这取决于新标记的数量和复杂性。

If you need to return a elaborate piece of HTML, it's always better to write it in the server side and returning it as data, it's also easier to maintain. Building complex HTML in the client side usually is cryptic, even with the use of modern js libraries. On the other hand, if your extra markup is small, then you can create it with js. I've never done anything with ASP.NET AJAX, but having a asp.net page, a rails view, or a JSP with only a small fragment like <p class='info'>Row Updated</p> its confusing.

如果你需要返回一个精心设计的HTML,最好将它写在服务器端并将其作为数据返回,它也更容易维护。即使使用现代的js库,在客户端构建复杂的HTML通常也很神秘。另一方面,如果您的额外标记很小,那么您可以使用js创建它。我从来没有对ASP.NET AJAX做过任何事情,但是有一个asp.net页面,一个rails视图,或者只有一个像

Row Updated 这样的小片段的JSP令人困惑。

Let the code speak to you, If you're fighting against javascript code to create markup in the client side, maybe it should go in the server side.

让代码跟你说话,如果你正在反对javascript代码在客户端创建标记,也许它应该放在服务器端。

Lastly: don't worry too much about the size of HMTL vs JSON, and if you do, benchmark the requests to see if the difference isn't negligible.

最后:不要太担心HMTL与JSON的大小,如果你这样做,请对请求进行基准测试,看看差异是否可以忽略不计。

#4


1  

I believe the more common method is to pass the mass of markup (HTML/CSS) via synchronous navigation to site, and keep the AJAX request/response as lean as possible [citation needed].

我相信更常见的方法是通过同步导航将大量标记(HTML / CSS)传递到站点,并使AJAX请求/响应保持尽可能精简[需要引证]。

Admittedly, it is pretty rare to see raw HTML returning as an AJAX response. Usually it will be a JSON response, as that is the easiest to eval() with JS.

不可否认,将原始HTML作为AJAX响应返回是非常罕见的。通常它将是一个JSON响应,因为这是使用JS最简单的eval()。

Since already most of the markup and styling classes are needed anyway, and might be used by "static" content as well, that leaves you the opportunity to keep the AJAX small, neat and to the point.

由于大多数标记和样式类都是必需的,并且也可能被“静态”内容使用,这使您有机会保持AJAX的小巧,整洁和重点。

#5


0  

Usually when updating small parts of screen you can send the JSON back to the client and the client can easily update itself. If you are trying to build a complicated Grid then better to use an UpdatePanel.

通常在更新屏幕的小部分时,您可以将JSON发送回客户端,客户端可以轻松更新自己。如果您正在尝试构建复杂的Grid,那么最好使用UpdatePanel。