Why is it a bad practice to return generated HTML instead of JSON? Or is it?
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.
Which side are you on and why?
I'm a bit on both sides, actually :
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 :
I generally don't really take into consideration the "performance" side of things, at least on the server :
Finally, one thing that definitly matters :
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 ;-)
I mainly agree with the opinions stated here. I just wanted to summarize them as:
It is bad practice to send HTML if you end up parsing it client-side to do some calculations over it.
It is bad practice to send JSON if all you'll end up doing is to incorporate it into the page's DOM tree.
Well,
I'm one of those rare persons that likes to separate things this way: - The server is responsible for delivering data (model); - The client is responsible for showing (view) and manipulating data (model);
So, the server should focus on delivering the model (in this case JSON is better). This way you get a flexible approach. If you want to change the view of you model, you keep the server sending the same data and just change the client, javascript components, that change that data into a view. Imagine, you have a server delivering data to mobile devices as well as desktop apps.
Also, this approach increases productivity, since the server and client code can be built at the same time, never losing the focus which is what happens when you keep switching from js to PHP / JAVA / etc.
Generally, I think most people prefer to do as much as possible on the server side because they don't master js, so they try to avoid it as much as possible.
Basically, I have the same opinion as those guys that are working on Angular. In my opinion that is the future of web apps.
链接地址: http://www.djcxy.com/p/1302.html上一篇: JSON:为什么正斜杠会逃脱?