<p>I'm looking for (arguably) the correct way to return data from a <code>XmlHttpRequest</code>. Options I see are:</p>

<ul>
<li><p><strong>Plain HTML</strong>. Let the request format the data and return it in a usable format.<br> 
<em>Advantage</em>: easy to consume by the calling page.<br><em>Disadvantage</em>: Very rigid, stuck with a fixed layout.</p></li>
<li><p><strong>XML</strong>. Let the request return XML, format it using XSLT on the calling page.<br><em>Advantage</em>: the requested service is easily consumed by other sources.<br><em>Disadvantage</em>: Is browser support for XSLT good enough?</p></li>
<li><p><strong>JSON</strong>. Let the request return JSON, consume it using javascript, render HTML accordingly.<br><em>Advantage</em>: easier to 'OO-ify' the javascript making the request. <br><em>Disadvantage</em>: Probably not as easy to use as the previous two options.</p></li>
</ul>

<p>I've also thought about going for option one while abstracting the view logic in the called service in such a way that switching in and out different layouts would be trivial. Personally I think this option is the best out of three, for compatibility reasons.</p>

<p>While typing this, I got another insight. Would it be a good idea to allow all three response formats, based on a parameter added to the request?</p>

[{'answer_id': 21997, 'author': 'Erlend Halvorsen', 'author_id': 1920, 'author_profile': 'https://Stackoverflow.com/users/1920', 'pm_score': 0, 'selected': False, 'text': '<p>I think this sort of depends on the level of "ajaxyness" your app is going to have. If your front end is a "rich client", al\'a gmail, I\'d go with the JSON solution, as you\'d have to solve the problem of having client side view generation anyway. If you\'re using ajax sparingly, to provide simple messages to the user, update a few fields now and then, etc, then I\'d go with option 1, since most of your view logic is already on the server.</p>\n'}, {'answer_id': 22002, 'author': 'Hrvoje Hudo', 'author_id': 1407, 'author_profile': 'https://Stackoverflow.com/users/1407', 'pm_score': 0, 'selected': False, 'text': "<p>Different response format shouldn't be difficult to produce.\n<code>JSON</code> works best for me, i like to keep <code>OO</code> in <code>js</code>, and don't know how to parse XML well :)</p>\n"}, {'answer_id': 22005, 'author': 'cringe', 'author_id': 834, 'author_profile': 'https://Stackoverflow.com/users/834', 'pm_score': 3, 'selected': True, 'text': '<p>If you\'re looking for a quick solution that should work with most available frameworks, I\'d go for JSON. It\'s easy to start with and works.</p>\n\n<p>If you\'re trying to build a larger application that you\'re going to extend (in terms of size or maybe your own API for 3rd party extensions) I\'d go for XML. You could write a proxy to provide the information in JSON or HTML too, but having XML as the main source is definitly worth the time and effort while building the app.</p>\n\n<p>Like <a href="https://stackoverflow.com/users/1450/john-topley">@John Topley</a> said: it depends.</p>\n'}, {'answer_id': 22010, 'author': 'Ian Oxley', 'author_id': 1904, 'author_profile': 'https://Stackoverflow.com/users/1904', 'pm_score': 2, 'selected': False, 'text': '<p>I\'d agree with John Topley - it depends on the application. There\'s a good article on quirksmode that discusses the advantages and disadvantages of each format that you might want to read: <a href="http://www.quirksmode.org/blog/archives/2005/12/the_ajax_respon.html" rel="nofollow noreferrer">http://www.quirksmode.org/blog/archives/2005/12/the_ajax_respon.html</a></p>\n'}, {'answer_id': 22559, 'author': 'JasonMichael', 'author_id': 1935, 'author_profile': 'https://Stackoverflow.com/users/1935', 'pm_score': 0, 'selected': False, 'text': '<p>I think trying to use XmlHttpRequest will be a huge headache, unless its the type of headache you don\'t mind - to do it properly you\'re almost reinventing the wheel.   Then again, people like to reinvent wheels in their spare time, just to say, "Hey, I did it".  Not me...</p>\n\n<p>I would get a framework like prototype or Extjs, that has alot of data loading functions built in for XML and JSON, plus you\'ll get more predictable results, as the frameworks have event handlers to make sure your XmlHttpRequest succeeded or failed.  Plus you get support for all the various browsers.</p>\n'}]
['https://Stackoverflow.com/questions/21992', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/909/']