Stefano
Stefano

Reputation: 3259

Javascript - Json or html?

i have an asp.net mvc view that with the help of jquery ajax updates a div. Should i use a controller that return a PartialView or json? And the performances?

Upvotes: 5

Views: 510

Answers (7)

Dan Roberts
Dan Roberts

Reputation: 4694

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.

Upvotes: 2

Alexander Bird
Alexander Bird

Reputation: 40599

This ... question ... has ... been ... asked ... before :)

Upvotes: 11

rsenna
rsenna

Reputation: 11964

To me, the coolest way of doing AJAX is returning slim data packets, not pre-rendered HTML. So I would say go with the JSON option...

... But it all depends of your needs. It is much easier to show pre-rendered HTML snippets, so if you need something fast a partial view may be a better option.

But all this has been discussed before. So take a look at this link: The AJAX response: XML, HTML, or JSON?. The author presents a good, platform independent summary of the 3 main AJAX response options, with advantages and disadvantages of each one.

Upvotes: 2

ChaosPandion
ChaosPandion

Reputation: 78252

If you are compressing the response then return HTML otherwise return JSON and build the HTML client side. In an ideal world your server should always compress the response but you may have a poorly configured server that is not under your control. The point of my answer however is to keep it simple because on the server side the difference between building HTML and JSON is quite minimal. Also building HTML on the client can be a bit slow for users with outdated machines. (Or browsers... you know the browser I am talking about...)

Note: My answer should be understood in the most general context. Without looking at your technology stack and target market it would be wrong of me to answer in any other way. Remember that most rules in programming are more like guidelines.

Upvotes: 1

Bitsplitter
Bitsplitter

Reputation: 980

Returning HTML most probably requires more server-side processing because the server will have to do the HTML formatting. Returning JSON will require more client-side processing because the client will have to format the HTML. Performance is thus a matter of where you'd like the processing to take place. If your server is under high load, you might want to offload some of the processing to the clients.

Upvotes: 1

linusthe3rd
linusthe3rd

Reputation: 3566

It depends on what you are trying to achieve. If you are creating a "thick" client (i.e. the server just processes data and the browser handles the rest), then the server should respond back w/ JSON.

It sounds like for your case, the page is already loaded. Considering this, JSON is the way to go and then have your JS handle the processing of the UI. There are no major performance trade-offs from using either method, but the best practice IMO would be to use JSON.

Upvotes: 0

scunliffe
scunliffe

Reputation: 63580

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.

Upvotes: 3

Related Questions