JSON与HTML Ajax响应 [英] JSON vs HTML Ajax response

查看:99
本文介绍了JSON与HTML Ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更快,返回JSON中的ajax,然后处理JSON响应以呈现html,或者让Ajax响应中的原始html放在一堆<li></li>中?

Which is faster, to return ajax in JSON and then process JSON response to render the html, or just have the Ajax response the raw html in a bunch of <li></li>'s?

推荐答案

取决于.在这两种情况下,服务器都只是返回带有文本的响应.如果响应的JSON版本比HTML版本需要更多的字符,则该响应将需要更长的时间才能传输回客户端,反之亦然.

Depends. In both cases, the server is simply returning a response with text. If the JSON version of the response requires more characters than the HTML version, that response will take longer to be transmitted back to the client, and vice versa.

但是,当然还有服务器端脚本必须完成其工作.在您的情况下,从服务器端脚本生成JSON可能比HTML更快.我没办法知道.

But of course there is also the server-side script which must do its work. Perhaps in your case generating JSON is faster than HTML from your server-side script. No way for me to know.

然后是客户端处理.您必须解析响应才能将其转换为真实的对象,然后您需要遍历结果对象以生成HTML.这肯定比将HTML响应并将其注入DOM所需的时间更长.

And then there is the client-side processing. You'd have to parse the response to turn it into a true object, and then you'd need to iterate over the resulting object in order to generate the HTML. This will definitely take longer than just taking an HTML response and injecting it into the DOM.

但是,我怀疑性能差异是否会很明显,这意味着您决定提供JSON响应还是HTML响应应该基于其他因素.

However, I doubt that the performance difference will be noticeable, meaning that your decision about providing a JSON response vs. HTML response should be based on other factors.

这篇关于JSON与HTML Ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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