G
G
gog692016-08-03 10:29:27
JavaScript
gog69, 2016-08-03 10:29:27

Render on the server or client side. for an AJAX response?

There is a page with a certain list at the bottom of which is the Load More button, which makes an AJAX request
, so I think to form a response in REST style and render it through JS and JS template or make an html response and just $(container).html(responseHTML);

but at the same time I reviewed popular sites - many make an html response. The

1st method is certainly more correct, but are the troubles associated with it justified?
in the 2nd method I don’t see anything wrong, extra bytes of html - in our time the weather will not play

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vyacheslav, 2016-08-03
@Firik67

In method 2, the bad thing is that if you need to send this data somewhere else, you will have to redo the entire render. The first way is universal. Get the data and do whatever you want with it.

M
Mikhail Osher, 2016-08-04
@miraage

content negotiation.
Send an Accept header and dance from it to the server.
If Accept is html, then return html, otherwise return json.
Flexible, comfortable.

H
hetzerok, 2016-08-03
@hetzerok

The first method is more correct in the sense that it is more universal. In addition, see if you need any additional control data in the response (for example, quantity, limit, offset, filters of some kind). If it is, then it's better to get confused and pass some JSON and process it in JS.

K
keltanas, 2016-08-04
@keltanas

If you write some REST-API to the site, then it is reasonable to receive JSON from the server and display it on the client.
But, since such questions arise, there is no API. Therefore, it is better not to bother and send plain HTML without layout. And if a request comes to the same url not from HttpRequest (see the title), but from a search engine, for example, or the user wants to open the link in a new tab, then send it from the layout.
Each method has its drawbacks, but there is no silver bullet. This one will be more efficient in this case.
If, as they say above, you want some kind of mobile application, then you still have to write an API with blackjack. But, this is a different level and a different budget. It is better to solve the problem, and not to invent something that does not exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question