Good practice with XHR requests

Asked

Viewed 248 times

5

I have the following doubt:

When I make a request via AJAX and I wish to "ride" the HTML based on data from a banco, is wrong (in terms of "good practice") to already return the HTML in the request itself?

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

(example: underscore.js)

I’m asking this because I saw that the Facebook, in some requisitions XHR, returns the HTML already in the request response itself. (and hence the question arises in the head: "If Facebook did, it is because it is right?")

  • 1

    I think this varies a lot from programmer to programmer and not much from good practices

  • 5

    I never mind good practice, actually tag has been removed a few times and should even go to the blacklist. It has no meaning, even more misspelled. Good practice is to do what is right in that particular situation. Without knowing the specific situations any indication of "good practice" is wrong. And the biggest problem is that people interpret them as something to always do. In this case, who is not specific, I would say that you should traffic data. If there is any reason in your composition exist tags HTML, do.

  • 1

    I have my doubts if it can answer without incurring opinion, if there is any real reason to choose one or the other.

  • 1

    @Marcelo, The problem of "varying from programmer to programmer" is that you never have a pattern (this no PHP, Believe me, it’s a very big problem!)

  • @Wallacemaxters, but this is unfortunately in all programming environments and etc... For example when programming a CLP, microprocessador, vc tbm does not have a well defined pattern, its programming will depend on what the producers of that CI decided to adopt when assembling the architecture of the same... it goes from the lowest level of programming to the highest level

  • The only good practices I can think of are not using synchronously and not doing parse of the return with eval.

  • @Marcelobonifazio, so we can say roughly that the "design patterns" serve only to decrease the number of gambiarras? Because we really always have the problem of something being out of the standard (even if it’s not a design standard, but the standard applied to that specific application)

  • 2

    @Wallacemaxters "me" would say yes, it depends on the project, on your need, on the "good practices of your company".... but as @bigown said, it’s hard to answer sem incorrer em opinião, both in relation to good practice, and the very term of what comes to be good practice

Show 3 more comments

1 answer

4


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

No, it is not wrong to return HTML directly

The object making the request 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 much of your application’s HTML is generated dynamically on the server, it is consistent to also generate what is obtained by AJAX requests.

Yes, it is wrong to return HTML directly

If your application is heavily based on a client-side template engine, it makes no 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 it is not in accordance with the standards of your application.

  • 2

    In the case of facebook, they return the JSON, where one of the properties of this JSON is an HTML. type: {_id : 1, content: "<div>text</div>"}. So they fall so much into the yes as in the nay

  • For you to see how subjective these "good practices" are. They probably have a reason to do this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.