2
$.post('somescript.php', { data: $('#myInputField').val() },
    function(replyData) {

1) Is the second argument of this $.post method - in json?

OR

2) Is the second argument of this $.post method a query string?

Thanks in advance, MEM

Note: If this question doesn't make sense, please, knowing why (it doesn't make sense) may also help and, be taken as an valid answer as well.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
MEM
  • 30,529
  • 42
  • 121
  • 191

1 Answers1

2

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 How do I build a JSON object to send to an AJAX WebService? 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.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg, but what I'm not getting is this: { data: $('#myInputField').val() } is a json format or not? If it is, they we are working with a json formated value yes? However, if that was the case we wouldn't have a need for stringify would we? :s – MEM Aug 23 '10 at 12:03
  • `$('#myInputField').val()` is any data. `JSON.stringify($('#myInputField').val())` is a JSON string. The data which will be posted looks like `data=blabla`, where `blabla` are JSON string which can be encoded a little with respect of `encodeURIComponent` function. On the server side you should get parameter with the name 'data' and decode there on the reverse way. – Oleg Aug 23 '10 at 12:25
  • You should decide yourself whether in some situation it would be better for you to post JSON data to the server or not. There are no best solution. In general if you want to post a complex data object to the server or the server **require** JSON data as the input you post JSON data. In case of Microsoft ASMX web service (see url which I posted in my answer) you **have to** post JSON data to the server if you want have JSON reply. – Oleg Aug 23 '10 at 12:28
  • Sorry. Now I get lost. Please, a **yes** or **no** question: **Is this in json format?** { data: $('#myInputField').val() } (By following a yes / no approach I will get my answer so I hope) :) – MEM Aug 23 '10 at 13:19
  • No. JSON format is described on http://www.json.org/. You can insert any text on the site http://www.jsonlint.com/ to verify whether the string is a correct JSON string. Typical JSON string is `{"x": "my x data", "y": 123}`. You can use `JSON.Parse` function which is internal for the most web browsers to convert any JSON string to the JavaScript object. Like `var myJson='{"x": "my x data", "y": 123}'; var myObj=JSON.Parse(myJson); alert(myObj.x);` – Oleg Aug 23 '10 at 13:33
  • Thanks for your patience. They seemed so similar that I thought they were the same. Clear now. :) So that is just a formatted way for jquery to understand key/value pairs, reply if you feel like: Is that way of formatting the key/value pairs what they call on jquery documentation as a "Map" ? cf. http://docs.jquery.com/Post – MEM Aug 23 '10 at 13:45
  • `var t = { data: $('#myInputField').val() };` is the short form in JavaScript to create an object which has one property with the name `data` (because I assigned it to the variable `t` I can easy access the property with `t.data`. The value of the property `t.data` will be initialized with `$('#myInputField').val()`. The form `{ data: $('#myInputField').val() }` looks close to the form used in JSON, but it is **NOT** the same. One suggested the special format "JSON" to make serialization/deserialization of objects to/from a string easy. See http://en.wikipedia.org/wiki/JSON for details. – Oleg Aug 23 '10 at 13:45
  • With JSON you can format not only key/value pairs maps, but much more complex nested structures. Some implementation of JSON serialize allows serialize recursive data types. So JSON is a form of representation (encoding) of general objects in form of strings. – Oleg Aug 23 '10 at 13:50
  • Is a "Map String" the same as "key/value pair string" ? :) In jQuery documentation we have, for our second argument: "data (Optional) Map, String" I'm not getting what Map is. :s – MEM Aug 23 '10 at 13:56
  • From: http://api.jquery.com/jQuery.ajax/ Topic: (Send Data To the Server) "The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string before it is sent." – MEM Aug 23 '10 at 14:40
  • Sorry, I don't understand exactly whether now everything is clear for you how to use `$.post`? You can use the second parameter of `$.post` exactly like the `data` parameter of `$.ajax`: either as a string, or as object. If the data has type `string` then the data will be send without any transformation. If the data are not a string the data will be converted to string with respect of `$.param` function (see http://api.jquery.com/jQuery.param/). The function supports also deep objects recursively serialization and not only "map" form like {key1: 'value1', key2: 'value2'}. But it's details only – Oleg Aug 23 '10 at 16:21
  • "don't makes any JSON encoding of data for you"..? – contactmatt Sep 07 '12 at 18:41
  • @contactmatt: It's depend on data which need be send. `$('#myInputField').val()` return string which can be easy send directly. Only if you have constructed data (object, array) you should use `JSON.stringify` to convert the data to string in JSON format. – Oleg Sep 07 '12 at 18:48