喵-见缝插针 2009-08-16 14:06 采纳率: 0%
浏览 461
已采纳

为什么返回生成的 HTML 而不是 JSON 是一种不好的做法? 是吗?

It is quite easy to load HTML content from your custom URLs/Web services using JQuery or any other similar framework. I've used this approach many times and till now and found the performance satisfactory.

But all the books, all the experts are trying to get me to use JSON instead of generated HTML. How's it much more superior than HTML?

Is it very much faster?
Does it have a very much lesser load on the server?

On the other side I have some reasons for using generated HTML.

  1. It's simple markup, and often just as compact or actually more compact than JSON.
  2. It's less error prone cause all you're getting is markup, and no code.
  3. It will be faster to program in most cases cause you won't have to write code separately for the client end.

Which side are you on and why?

转载于:https://stackoverflow.com/questions/1284381/why-is-it-a-bad-practice-to-return-generated-html-instead-of-json-or-is-it

  • 写回答

14条回答 默认 最新

  • 七度&光 2009-08-16 14:11
    关注

    I'm a bit on both sides, actually :

    • When what I need on the javascript side is data, I use JSON
    • When what I need on the javascript side is presentation on which I will not do any calculation, I generally use HTML

    The main advantage of using HTML is when you want to replace a full portion of your page with what comes back from the Ajax request :

    • Re-building a portion of page in JS is (quite) hard
    • You probably already have some templating engine on the server side, that was used to generate the page in the first place... Why not reuse it ?

    I generally don't really take into consideration the "performance" side of things, at least on the server :

    • On the server, generating a portion of HTML or some JSON won't probably make that much of a difference
    • About the size of the stuff that goes through the network : well, you probably don't use hundreds of KB of data/html... Using gzip on whatever you are transferring is what's going to make the biggest difference (not choosing between HTML and JSON)
    • One thing that could be taken into consideration, though, is what resources you'll need on the client to recreate the HTML (or the DOM structure) from the JSON data... compare that to pushing a portion of HTML into the page ;-)

    Finally, one thing that definitly matters :

    • How long will it take you to develop a new system that will send data as JSON + code the JS required to inject it as HTML into the page ?
    • How long will it take to just return HTML ? And how long if you can re-use some of your already existing server-side code ?


    And to answer another answer : if you need to update more than one portion of the page, there is still the solution/hack of sending all those parts inside one big string that groups several HTML portions, and extract the relevant parts in JS.

    For instance, you could return some string that looks like this :

    <!-- MARKER_BEGIN_PART1 -->
    here goes the html
    code for part 1
    <!-- MARKER_END_PART1 -->
    <!-- MARKER_BEGIN_PART2 -->
    here goes the html
    code for part 2
    <!-- MARKER_END_PART2 -->
    <!-- MARKER_BEGIN_PART3 -->
    here goes the json data
    that will be used to build part 3
    from the JS code
    <!-- MARKER_END_PART3 -->
    

    That doesn't look really good, but it's definitly useful (I've used it quite a couple of times, mostly when the HTML data were too big to be encapsulated into JSON) : you are sending HTML for the portions of the page that need presentation, and you are sending JSON for the situation you need data...

    ... And to extract those, the JS substring method will do the trick, I suppose ;-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(13条)

报告相同问题?

悬赏问题

  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖