D
D
driverx182018-01-22 01:35:52
AJAX
driverx18, 2018-01-22 01:35:52

Return data in plain text, JSON, or XML?

I send an Ajax request to the server to take some data, and when it comes time to return a response, I just take it and in php I shove the array that is returned into return json_encode. Further in JS I accept and do JSON.parse.
However, according to my observations, it was possible to return plain text (return array), and also get it in JS.
So, what's the real difference? The fact that JSON is better than XML is clear, but the question now is between returning data in plain text and JSON

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2018-01-22
@driverx18

json you will immediately convert it into a js-object and pull the necessary data directly and / or through variables. and the text needs to be parsed.
in other words:
- if you're getting a dataset, it's better to use json/xml.
- if the whole answer is the only "given" one (when the whole answer can be taken at once and inserted somewhere), then I don't care what format.

D
Danil Razumkov, 2018-01-22
@razumkov2015

JSON is a well-established standard, but with the text, FIG knows what errors will be. If the answer from the north implies any information, i.e. it's not: ok, yes, 200, success, etc., then use JSON.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question