1

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:

  1. Ask the server for a JSON object (and then I will create the table using Javascript)
  2. 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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Alon
  • 7,618
  • 18
  • 61
  • 99
  • One advantage to using JSON as opposed to HTML, in the case of a web service, for example, would be that it can then be interpreted more easily by whatever client is calling it, as opposed to HTML, which tends to be more cumbersome to pick the data out of. – Nightfirecat Jul 10 '12 at 07:28
  • 3
    There are about a dozen identical questions on this topic. Have you looked at them? If so, what specifically do you want to know that is not in the other answers? (e.g. http://stackoverflow.com/questions/1148266/ajax-html-vs-xml-json-responses-perfomance-or-other-reasons http://stackoverflow.com/questions/4171167/jquery-ajax-return-json-or-plain-html-better http://stackoverflow.com/questions/3353434/do-you-ajax-json-or-html etc. – njr101 Jul 10 '12 at 07:30

3 Answers3

2

It totally depends on your usage. when you need html rightaway and feel easy with returning html from server side. Use HTML and if you feel to play with json objects to create your required output, use Json.

Obviously HTML has larger data than json

Raab
  • 34,778
  • 4
  • 50
  • 65
1

It really depends. Each method has its pros and cons.

  • JSON

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.

  • HTML

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

What constraints do you have? Who would access your website? If it can be accessed from mobile phones and slow PCs then you have to go for option 2. If it would be accessed only from good corporate PCs then you can choose option 1. Then again, if your application scales good on commodity servers, then you still can go for an option 2.

Dmitry Osinovskiy
  • 9,999
  • 1
  • 47
  • 38