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.
- It's simple markup, and often just as compact or actually more compact than JSON.
- It's less error prone cause all you're getting is markup, and no code.
- 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?
Best Answer
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 :
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 ;-)