ajax html vs xml / json响应 - 性能或其他原因 [英] ajax html vs xml/json responses - performance or other reasons

查看:98
本文介绍了ajax html vs xml / json响应 - 性能或其他原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当ajax重的网站,一些3k html格式的网页从ajax请求插入到DOM中。



我一直在做的是html响应,并使用jQuery插入整个事物。

我的其他选项是以xml(或可能是json)输出,然后解析文档并将其插入到页面中。

我注意到,大多数大型网站似乎都是以json / xml方式进行操作。 Google Mail会返回xml而不是格式化的html。

这是由于表现吗?还是有另一个原因使用XML / JSON VS只是检索HTML?



从JavaScript的角度来看,它似乎注入直接HTML是最简单的。在jQuery中,我只是这样做的

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

带一个xml / json响应我必须做的

  jQuery.ajax({
类型:POST,
url:getpage.php,
data:requestData,
成功:函数(xml){
$(message,xml).each(function(id){
message = $(message,xml).get(id);
$(#messagewindow)。prepend(< b> + $(author,message).text()+
< / b> ;:+ $(text ,message).text()+
< br />);
});
}
});

从代码的角度来看,显然效率不高,我不能指望它是更好的浏览器性能,所以为什么要做第二种方式?


b
$ b

由于数据仍然只是数据,因此您可以选择如何将其显示给客户端。这允许很多代码在客户端执行而不是在服务器端执行 - 服务器端只需要知道数据结构,而不需要关于标记。所有的程序员需要知道的是如何提供数据结构。



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



如果要构建另一个客户端(不使用HTML作为标记语言),所有服务器组件可以重用。构建另一个服务器实现也是如此。


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

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

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

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

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

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);
    }

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 />"); 
        });
    }
});

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?

解决方案

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

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.

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.

这篇关于ajax html vs xml / json响应 - 性能或其他原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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