In general both ways are in practice very close. The function $.post
or $.ajax
will encode the data posted in the same way. If you want to post JSON data you should additionally encode the data value with some JSON encoder. See http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086 as an example ($.post
is a short form of $.ajax
, so all which described with $.ajax
and correct also for $.post
)
$.post('somescript.php', { data: JSON.stringify($('#myInputField').val()) }, ...);
In the code above I use JSON.stringify
from http://www.json.org/js.html.
UPDATED: After your questions in the comment I hope I understand more which you want to know. So jQuery.post don't makes any JSON encoding of data for you for and input parameters (the second parameter of jQuery.post
). So it send the data always exactly in the same way. You can add additional "json" parameter (the last dataType
parameter) to the $.post
call, but this will not change how the data will be encoded.
The question "should I send JSON data to the server or not?" is independent on $.post
and you should answer on the question yourself depend on the requirement existing in your project. Sometime it is the question of the architecture of your solution. Sometime you have to choose one special way.
In case of Microsoft ASMX Web Service for example there exist some important restriction. For example you want to deliver JSON data from the web service to be able to work easy with the data in JavaScript. So you want to have a method on the server side which have some input parameters and returns JSON as output. In case ASMX Web Service you must sent all input parameter to the web service method as JSON encoded data to be able to return JSON data from the web service, but ASMX Web Service decode/encode the data for you and you don't need manually encode/decode JSON on the server side.