ajax html vs xml/json responses - performance or other reasons
Returning JSON/XML gives the application more freedom compared to returning HTML, and requires less specific knowledge in different fields (data vs markup).
Since the data is still just data, you leave the choice of how to display it to the client side of things. This allows a lot of the code to be executed on the client side instead of on the server - the server side needs to know only about data structures and nothing about markup. All the programmer needs to know is how to deliver data structures.
The client implementation only needs to know about how to display the data structures returned by the server, and doesn't need to worry about how these structures actually get build. All the programmer needs to know is how to display data structures.
If another client is to be build (that doesn't use HTML as a markup language), all the server components can be reused. The same goes for building another server implementation.
Here are a few pros for sending JSON/XML instead of HTML:
- If the data is going to ever be used outside of your application HTML might be harder to parse and fit into other structure
- JSON can be directly embedded in
script
tags which allows cross domain AJAX scenarios - JSON/XML preserves the separation of concerns between the server side scripts and views
- Reduces bandwidth