Sending variables from client to server | JSON vs POST

If I say 1-5 client-side variables that are needed on the server side and I process my messages through ajax (Post method), there are two main ways I can get them.

  • Using JSON for encoding (and subsequent decoding) and sending as a JSON string in the POST variable

or

  • Just create a POST variable for each variable I need to send.

Does it matter or is it just preference?

+4
source share
4 answers

There will certainly be overhead for JSON deserialization, but not much. I believe that JSON is much preferable to create new variables for each value you want to send. Also, I find that as soon as your JSON implementation is in place, scaling is much easier (i.e. when you have to add new values ​​later for any reason).

+1
source

I think it all comes down to the amount of overhead that json will add. If you are using POSTING 2,3 or about 5 fields, just use regular POST. Otherwise, if you are trying to convey some significant amount of meaningful data, it is always useful to use JSON.

Basically, for large amounts of data that are necessary for computing on the server, it is best to use JSON from the moment it is decoded, it will give an array of arrays, and it will just be easier, and not ride a bike through the $ _POST array.

In addition, encoding in JSON and sending information to the server makes sense, especially when the server will perform calculations / checks and send a response. However, it is always better to send responses encoded in JSON instead of plain text. Adding / editing data in POSTED data becomes easier when using JSON.

+2
source

I think this is only a personal preference.

I have not heard anything special about performance or best practice, for example when using JSON with POST. When POSTing forms from an AJAX call, I prefer to use the usual POST parameters, if their account is not very inconvenient for me to write each from the bottom - then I use JSON ...

But it must be unified for each project - after you decide to use JSON, always use it ...

+1
source

Well, the good thing about json is the encapsulation and api that it provides. But then there might be a bit of overhead on serialize / unserialize, compared to just sending the values ​​as is.

In the end, it does not really matter.

+1
source

Source: https://habr.com/ru/post/1415192/


All Articles