views:

129

answers:

1
$.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.

+1  A: 

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.

Oleg
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
`$('#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
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
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
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
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
`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
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
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
MEM
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