Good practices with XHR requests

5

I have the following question:

When I make a request via AJAX and I want to "mount" the HTML based on data coming from a banco , it is wrong (in terms of "good practices") to already return HTML on itself requisition?

Or is it better (as I currently do) to return the data in JSON and use a template engine in javascript to process this data for DOM ?

(example: underscore.js )

I'm asking this because I've seen that Facebook , in some requests XHR , returns the HTML already in the request response itself. (and then the question comes up in the head: "If Facebook did, is that right?")

    
asked by anonymous 23.01.2015 / 14:20

1 answer

4

As pointed out by other users in the comments above, there are no "best practices" except in relation to the specific context of each application. So the answer is yes and not .

No, it is not wrong to return the HTML directly

The requesting object was created to get XML, but can actually return anything, including HTML. There is no problem in doing this if it makes sense within your application. For example, if a good part of the HTML of your application is generated dynamically on the server, it is also coherent to generate what is obtained by AJAX requests.

Yes, it is wrong to return the HTML directly

If your application is heavily based on a client-side template engine, it does not make sense to stop using it in a specific situation, unless you have a good reason to do so. That is, it is wrong to return HTML if this does not conform to your application's standards.

    
23.01.2015 / 16:07