To simple questions perhaps: jquery.post second argument - what format?

Hello,


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

From jquery documentation:

data (Optional)Map, String Key/value pairs or the return value of the .serialize() function that will be sent to the server.

I don’t understand.

  1. What kind of data is Map?

  2. That string containing a key/value pair should be inserted in json format?
    No. And it isn’t in json format. Similar however. (update)

  3. Is that string a query string? In the same sense we call query strings when we talk about GET method usage?

Thanks in advance,
Márcio

1 Like

Please be precise.
I’m newbie, and because of that, precision is precious to not follow bad tracks and get it all wrong at once… :wink:

If they are the same, why do we need to mention both on jquery manual, and why are they presented as an alternative to each other?
I believe they are not the same.

No. (ehehe) I believe it isn’t they are different things. When we have this as a paramters we are talking about an object literal with key/value {} and NOT json formated string.
They are different so I re-believe. :slight_smile:

Sorry, I can’t see the connection. I don’t see where that answers the question.
I mean:

Now I know that:

In the $.post() method jQuery converts an object literal to a query string, in name/value pairs

Nice.

According to this, I then ask:
“What are query strings in this sense”? I think that query strings are THINGS that are passed by URL using the GET method.
Here, we are using the POST method, hence, no passed values trough URL, so, probably, no query strings as well… But, since we DO talk about query strings, at least one of those options should apply.
Either:

a) We are talking about two types of query strings (a js/jquery kind, and a GET type of them).

OR

b) Even if we do use POST we STILL pass query strings.

A) or B) ? Or is there a C) that I’m not aware off? :wink:

Regards,
Márcio

  1. What kind of data is Map?
    -> string data in key->value pair or mapping

  2. That string containing a key/value pair should be inserted in json format?
    No. And it isn’t in json format. Similar however. (update)
    -> yes can be I think, since json format is still a string but not advisable as JSON has more than key->value pairing, e.g. multidimension array values which other means is more appropriate when passed as “GET” key/value pair.

  3. Is that string a query string? In the same sense we call query strings when we talk about GET method usage?

as requoted:
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’}.

I still don’t know what a map is, and why is it diferent from a string, still:

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.
from: http://api.jquery.com/jQuery.ajax/ (Sending Data to The Server).

Still applies. :frowning:

$.post(‘somescript.php’, { data: $(‘#myInputField’).val() },
function(replyData) {});

mapping is equal to…

somescript.php?data=myinputfieldvalue
or
$.post(‘somescript.php’, { key1:“val1”,key2:“val2” },
function(replyData) {});
where
key=> data
value=>myinputfieldvalue

when received on somescript.php

Sorry, can you please situate your answer into my question numbers. So that I can properly make the connection.
I’m a little lost. :frowning:

Thanks in advance.

I’m a newbie too… :wink:

If they are the same, why do we need to mention both on jquery manual, and why are they presented as an alternative to each other?
I believe they are not the same.

IMHO, they are the same…on my end i’d prefer passing object literal key-pair values than the original thru URL.
e.g.
$.post(“country.php”,{ country:“KSA”,city:“Jeddah”,district:“Rawdah”,street:“Sari”},function(){});

VS

$.post(“country.php?country=KSA&city=Jeddah&district=Rawdah&street=Sari”,function(){});

I’d prefer the latter of course especially if on key->value pair, value is dynamic…very easy to understand.

No. (ehehe) I believe it isn’t they are different things. When we have this as a paramters we are talking about an object literal with key/value {} and NOT json formated string.
They are different so I re-believe.

Actually I can passed minimal JSON data but in base64 encoded and again, there is a better/correct approach than passing it in “GET”, e.g. cookie(1Kb), Session(Server Side) but need to have a JS to fetch those value saved.

b) Even if we do use POST we STILL pass query strings.

Difference of POST vs GET is that on GET, query strings are shown in URL and on POST, it is not…try it in firebug, you’ll see all your data when pass either using methods.

Thank you for your help. :slight_smile: Ready to go.

Yupii!!! :slight_smile:

Márcio