將HTML標記作為JSON響應的一部分,它是最佳實踐嗎?

[英]Having HTML markup as part JSON response, is it a best practice?


Is it a good practice to have html markup as part of API response? which has the content-type as application/json

將html標記作為API響應的一部分是一種好習慣嗎?其內容類型為application / json

Sample json response

樣本json響應

{ html : '<div id="markup">This is the server response html.</div>', data : {property : 'This is the data received from the server.'} }

{html:'

這是服務器響應html。 ',data:{property:'這是從服務器收到的數據。'}}

1 个解决方案

#1


0  

It's not best practice.

這不是最好的做法。

Generally, you want to create a separation of concerns within your systems. By having your API return HTML, your server starts to take some responsibility for the presentation layer of your application as well as providing the data.

通常,您希望在系統中創建關注點分離。通過讓API返回HTML,您的服務器開始對應用程序的表示層負責,並提供數據。

Think about what would happen if in future, you want to build a mobile app using your server. It wouldn't be able to effectively make use of the html property. Would you add a separate property that gets used by the mobile app? You don't want to keep changing your API server each time a new client wants to use it.

想想如果將來要使用您的服務器構建移動應用程序會發生什么。它無法有效地利用html屬性。您是否會添加一個由移動應用程序使用的單獨屬性?每次新客戶端想要使用它時,您都不希望繼續更改API服務器。

Think about what happens if you get a designer on to help you with your application. They now need the ability to modify code on the API server as well as the web layer. If you keep presentation of the data separate to the raw data, you won't run into this problem.

想想如果讓設計師幫助您完成應用程序會發生什么。他們現在需要能夠修改API服務器以及Web層上的代碼。如果將數據的表示與原始數據分開,則不會遇到此問題。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/03/17/7251928a450c3d64566125829d65e62d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com