I
I
Ivan2016-08-10 13:58:24
HTML
Ivan, 2016-08-10 13:58:24

JSON vs. HTML (XML?). What to use in AJAX request?

Actually a subject.
Somehow, out of habit, I return the html code with an ajax request and insert / add it to the desired block. I know that it is possible to transfer JSON data, but to be honest, I don’t see the point in this, because it turns out double work - first you prepare the data on the server side, and then you parse it from JSON on the client into HTML.
What is the benefit of using JSON and where is it used then? Is it a matter of convenience for a particular developer?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yustas Alexu, 2016-08-10
@LiguidCool

JSON is raw data, not tied to the current implementation on the frontend. By giving HTML markup from the server, the developer binds the server side to a specific view on the frontend. If the design changes, the whole backend needs to be changed. JSON will be easier to work with in this case. In this case, the logic on the server can not be changed at all, but only the logic on the frontend can be changed.
Secondly, JSON data is smaller than HTML markup, which reduces server load and traffic.
Thirdly, generating HTML on the backend is an additional load on the server. It is better to load the client (browser), especially when it comes to a highly loaded service (high load).
Thirdly, by giving data in the form of JSON, you can reduce the number of requests to the server by combining data for different parts of html in one JSON. But with HTML markup, this will not work.
PS: Giving markup in an ajax request has long been considered bad form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question