阿贾克斯VS HTML XML / JSON响应 - 性能比较或其他原因 [英] ajax html vs xml/json responses - perfomance or other reasons

查看:111
本文介绍了阿贾克斯VS HTML XML / JSON响应 - 性能比较或其他原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当沉重的AJAX网站和一些3K HTML格式的网页插入来自Ajax请求的DOM。

I've got a fairly ajax heavy site and some 3k html formatted pages are inserted into the DOM from ajax requests.

我一直在做,走的是HTML响应,只是插入使用jQuery整个事情。

What I have been doing is taking the html responses and just inserting the whole thing using jQuery.

我的另一种选择是在XML(或可能JSON),然后解析文档,并将其插入到页面的输出。

My other option is to output in xml (or possibly json) and then parse the document and insert it into the page.

我发现似乎大多数大型网站做的事情的JSON / XML的方式。谷歌邮件返回XML,而不是使用HTML。

I've noticed it seems that most larger site do things the json/xml way. Google Mail returns xml rather than formatted html.

这是由于性能?或者是有其他理由使用XML / JSON VS只是检索HTML?

Is this due to performance? or is there another reason to use xml/json vs just retrieving html?

这是一个JavaScript的角度来看,这似乎直接注入HTML是最简单的。在jQuery的我就是这样做

From a javascript standpoint, it would seem injecting direct html is simplest. In jQuery I just do this

jQuery.ajax({
    type: "POST",
    url: "getpage.php",
    data: requestData,
    success: function(response) {
        jQuery('div#putItHear').html(response);
    }

使用XML / JSON响应,我会做

with an xml/json response I would have to do

jQuery.ajax({
    type: "POST",
    url: "getpage.php",
    data: requestData,
    success: function(xml) {
        $("message",xml).each(function(id) { 
            message = $("message",xml).get(id); 
            $("#messagewindow").prepend("<b>" + $("author",message).text() + 
            "</b>: " + $("text",message).text() + 
            "<br />"); 
        });
    }
});

显然不如从code的角度来看有效,我也没想到,这是更好的浏览器的性能,那么为什么做事情的第二个办法?

clearly not as efficient from a code standpoint, and I can't expect that it is better browser performance, so why do things the second way?

推荐答案

返回JSON / XML为应用程序提供更多的自由相比,返回HTML,需要在不同的领域不太具体的知识(数据VS标记)。

Returning JSON/XML gives the application more freedom compared to returning HTML, and requires less specific knowledge in different fields (data vs markup).

由于数据仍然仅仅是数据,你离开了如何显示事物的客户端的选择。这让很多的code将在客户端上,而不是在服务器上执行 - 服务器端只需要知道数据结构或一无所知的标记。所有程序员需要知道的是如何提供的数据结构。

Since the data is still just data, you leave the choice of how to display it to the client side of things. This allows a lot of the code to be executed on the client side instead of on the server - the server side needs to know only about data structures and nothing about markup. All the programmer needs to know is how to deliver data structures.

客户端实现只需要知道如何显示服务器返回的数据结构,而不需要担心如何将这些结构真正得到建立。所有程序员需要知道的是如何显示的数据结构。

The client implementation only needs to know about how to display the data structures returned by the server, and doesn't need to worry about how these structures actually get build. All the programmer needs to know is how to display data structures.

如果另一客户机是要建立(不使用HTML作为一种标记语言),所有的服务器组件可以重新使用。这同样适用于建筑的另一台服务器执行。

If another client is to be build (that doesn't use HTML as a markup language), all the server components can be reused. The same goes for building another server implementation.

这篇关于阿贾克斯VS HTML XML / JSON响应 - 性能比较或其他原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆