Some possibilities are JSON, XML, HTML and/or Javascript fragments. The democratic answer to this question is "it depends on the situation," but I am looking for why one may be better than the others.


I prefer JSON

  • Many many libraries out there for it in all languages
  • Doesn't require a complicated schema
  • Usually can be converted into an associate array or language equivalent quite easily. This is what makes JSON in PHP so easy
  • Compact: Not a lot of boilerplate
  • Easy to use: XML is notoriously hard to work with in any language, while JSON is very easy, especially if your language has associate arrays

When it comes to JSON vs. XML, I also prefer JSON (see TheLQs excellent answer), but when some people prefer XML for whatever reason, I'm fine with that.

Returning HTML can be okay in some situations, but I dislike it for a simple reason: it violates the MVC distribution of responsibilities; i.e. the controler does something (creating a layout) that should be up to the view.

Returning JavaScript fragments, except for the special case of JSON, is very questionable and should be avoided IMO. In my current project, I've done it once, but I consider it a bad idea and will probably refactor it soon. Doing that on a large scale creates systems that are hard to understand, difficult to debug and a nightmare to maintain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.