Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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