我应该返回HTML或JSON在阿贾克斯单页结帐HTML、JSON、阿贾克斯单页

2023-09-11 01:28:07 作者:配角戏、优雅退场。

我想用一个漂亮的单页结帐更新现有ASP.NET web表单的电子商务网站,我正在寻找关于如何更新数据的一些最佳实践。该页面将包括几个部分组成:购物车,用户识别,付款方式,交付选项等

I want to update a current ASP.NET webforms e-commerce site with a nice one-page checkout and I'm looking for some "best practice" on how to update the data. The page will consist of several parts: the cart, user identification, payment options, delivery options etc.

如果用户更改付款方式,这可能导致改变页面的其他部分,如交付选项不可用,总成本变化。

If the user changes the payment option this might result in changes in other parts of the page, like the delivery option becomes unavailable and the total cost changes.

我建立我的web服务,使他们返回完整的pre计算的HTML当用户改变一些东西在网页上?或者,我回某种秩序对象的JSON格式,并更新所有需要用JavaScript更新不同部分?

Do I build my webservices so they return the full pre-calculated html when the user changes something on the page? Or do I return some kind of order-object in json format and update all the different parts that need updating with javascript?

第二个选择似乎更清洁的我,但它是一个正常的页面速度太慢?或者它存在的第三种选择?

The second option seem cleaner to me but is it too slow for a normal page? Or does it exist a third option?

推荐答案

99.9%的时间,JSON会比等价的HTML更紧凑:更小的有效载荷意味着更快的交付响应。但是,一个更重要的原因是:

99.9% of the time, JSON will be more compact than equivalent HTML: smaller payload means quicker delivery of the response. But an even more important reason is:

如果不同的页面想打电话给你的web服务:一个人想将结果显示为一个列表,而其他想显示他们作为一个表,第三要在把结果放到一个jgGrid?如果你的web服务返回的HTML,2的3页不能使用你的服务。如果你的web服务返回的JSON,所有3这些页面可以很容易地使用它。

What if different pages want to call your webservice: one wants to display the results as a list while the other wants to display them as a table, and the third wants to put the results into a jgGrid? If your webservice returns HTML, 2 of those 3 pages can't use your service. If your webservice returns JSON, all 3 of those pages can use it easily.

如果有什么,部署2个月后,你的设计师/网站拥有者想从表中更改设计到一个列表?如果你的web服务返回的JSON,你的设计师可以进行必要的修改快速方便,可能受影响的只是1或2个已部署的系统中的文件。但是,如果你的web服务返回的HTML,你需要编译,测试和重新部署整个Web服务只是为了支持这一愚蠢的小布局的变化。

What if, 2 months after deployment, your designer/site-owner wants to change the design from a table to a list? If your webservice returns JSON, your designer can make the necessary changes quickly and easily, probably affecting only 1 or 2 files in the deployed system. However, if your webservice returns HTML, you need to compile, test, and redeploy the entire webservice just to support that silly little layout change.

有可能是时候返回HTML是一种必要的恶(没有,我能想到的副手,但我敢肯定有一些)。发生这种情况时,请你帮个忙,并确保HTML的Web服务是构建无需重新编译/重新部署整个服务的改变。一个严厉而有效的方法来做到这一点是使用AJAX调用一个ASPX页面。

There may be times when returning HTML is a necessary evil (none that I can think of off-hand, but I'm sure there are some). When that happens, do yourself a favor and make sure the HTML your webservice is building can be changed without a recompile/re-deploy of the entire service. One heavy-handed but effective way to do this is using ajax to call an ASPX page.

更新:我忘了说:即使在最慢的JavaScript引擎(InternetExplorer的),我总是发现这种技术(使用JS相应地解析JSON和更新页面)表现出众

Update: I forgot to mention: even on the slowest javascript engines (InternetExplorer), I've always found this technique (using JS to parse JSON and update the page accordingly) to perform very well.