HTML template + JSON vs Server HTML

后端 未结 2 843
情歌与酒
情歌与酒 2021-01-02 08:31

What do you think is better?

Use for Ajax result:

  1. HTML that was generated on server
  2. Return Data that would be used within template?
2条回答
  •  别那么骄傲
    2021-01-02 08:35

    Both approaches have pros and cons. Returning JSON or XML from the server and using javascript templating to convert to HTML is more RESTful and has the advantage of separating data and presentation and allowing multiple clients to easily use it. The cons is that it is more work to do in javascript.

    On the other hand if the server returns HTML all you have to do is inject it somewhere into the DOM. Unfortunately in this case markup and data are mixed and it would be more difficult for other clients to extract data without formating (imagine for example a desktop or mobile application that wants to consume services from your site).

    IMHO if the only consumer is your site then returning HTML would be the better approach.

提交回复
热议问题