I want to create an ajax call and get some data (let’s say a table with 100 rows). I have 2 ways of doing that:
- Ask the server for a JSON object (and then I will create the table using Javascript)
- Ask the server for the HTML – and then I don’t need to do anything on the client.
Is one option is absolutely better then the other? I assume that for huge amount of data it will be best to choose the JSON option. and for really small HTML pieces it will be best to ask for the HTML itself. but I am not sure about that and what to do in the average use-case – what will be the most efficient way.
It really depends. Each method has its pros and cons.
Pros: smaller size, interoperable
Cons: more difficult to build UI on the client compared to HTML although there are now client side templating frameworks that could help you.
Pros: very simple on the client, practically a single line of code:
$('#foo').html(result);Cons: non interoperable. If your client is a WPF application for example you will have parse this HTML to extract the information you are interested in in order to build your UI.
So depending on your requirements and environment you might decide to choose one or the other approach for your application.