T
T
Twist2014-11-04 19:57:07
PHP
Twist, 2014-11-04 19:57:07

Html vs json what to use?

I saw 2 approaches
1 in the Ajax response we give plain html to the client and simply display it (a huge minus in the transfer speed)
2 we give json and form html on the client (actually how .js frameworks do)
The first method is really very bad and should not be used ? (gcode, muveton)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
gmlexx, 2014-11-04
@bboytiwst

You don't have to worry too much about the transfer speed here. Usually gzip-content arrives nevertheless.
In the first approach, the server code, as a rule, is easier to write and the server has all the necessary information to produce the finished result in one request.
The disadvantage of the first approach and the advantage of the second is that the latter allows you to separate the server logic (backend) from the client (frontend). Development, layout, testing in this case can be carried out more independently, which means more efficiently and quickly when the project is large.
The disadvantage of the second approach is that it will require more Javascript code.

A
aen, 2014-11-04
@aen

You need to step back from the task. If you just need to put a piece of static, then it makes no sense to receive it with json and then dance with a template engine.

E
Elizaveta Borisova, 2014-11-04
@Elizaveta

Not so important, but you need to look at a specific application. And if the rendering of templates is frequent, you also need to monitor performance. In Angular, we can say, a mixed approach and pure (almost) html can come, it seems that this fact does not bother anyone.

S
Sergey Romanov, 2014-11-05
@Serhioromano

It all depends on what you do and what you use. For example, in Angular, I would not generate ready-made HTML on the server, since then the use of Angular as a whole loses its meaning.
If I do not use frameworks, then I have such a dilemma.
1. In ready HTML there are elements that are attached to JS events, buttons there, menus, ... Then JSON. Because if, for example, you use bootstrap and return HTML that has a tooltip, then they will not be shown until you apply a javascript update. Or if the HTML has a ready-made JS code, it cannot always be applied correctly.
2. Ready HTML is just pure text. Then HTML.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question