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?


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.


这个......问题......一直......被问......之前:)


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

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.

链接地址: http://www.djcxy.com/p/47890.html

上一篇: 如何使用servlets和jsp进行SPA?

下一篇: JSON或HTML?