Javascript - Json or html? Javascript - Json or html? ajax ajax

Javascript - Json or html?


I'd lean towards JSON myself. The reason is that JSON affords you the flexibility to bring back different data. e.g.

You might bring back 100 results, AND some metadata about what those results are (keywords, grouping, etc.) AND info about how many more results are available, how fresh the results are, paging info...

Likewise other un-related info might come back - status messages, "new mail!" indicators etc.

With pure HTML returned you limit further options to do anything other than update the HTML content of a container.


If you want the user be able to switch between different views, and this is data that can simply be included on page load, then just include onload then hide/show or load as json and then populate as needed.

If this is a very simple request that needs to be updated as needed and you don't plan on other similar needs in your app then just return html.

However if you are making a request that submits data or performs some action, I am a fan of returning a consistent json response with error boolean, error message and content/data elements.