I
I
Ivan2015-05-10 19:31:01
JavaScript
Ivan, 2015-05-10 19:31:01

Why is it better for Ajax to return JSON and not HTML?

I want to hear your arguments, besides that the initial data in pure JSON is smaller and, accordingly, will be transferred faster.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2015-05-10
Protko @Fesor

it all depends on the context.
From the point of view of the client-server architecture, this is correct, as it allows us to completely separate the client and server. The client will know nothing about the server implementation, the server knows nothing about the client. For SPA, that's it.
From the point of view of classic websites where everything is first rendered on the server, there are two options:
- if we need data from the server to update a little thing, which really depends on the current state of the page, that is, this is part of the presentation logic on the client , then json is better and on the client to do what you need. So we will simplify the logic on the server.
- If we just have to load something and performance on the client is important to us, then it makes sense to give rendered HTML. The html chunk does not load much longer than the json chunk, and in general, due to the lack of rendering on the client, we can even save money. So let's say twitter did (and maybe still does). But again, this complicates the support of the server, so here you need to look at whether it complicates, how much, etc.

E
Evgeny Petrov, 2015-05-10
@Petroveg

Correct? There is no such definition in relation to the answer to your question.
It all depends on how you deal with the data on the client.
For example, you put down values ​​in form fields. Why do you need html ?
For example, you simply paste the code into the informer. Why do you need json ?
For some tasks, just text/plain will do . And some services exchange xml .

A
Andrey Andreev, 2015-05-10
@b0nn1e

IMHO, more features, you can transfer the status, errors and other service information, and shift the logic to JS. What can't be done in html. You can shove html into json, but shove additional information into html is a hassle.
In general, it all depends on the task, sometimes it's easier to drive html right away and paste it where you need it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question