SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Hello,

    Code JavaScript:
    $.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

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Location
    Kingdom of Saudi Arabia
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $.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

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, can you please situate your answer into my question numbers. So that I can properly make the connection.
    I'm a little lost.

    Thanks in advance.

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    1) What kind of data is Map?
    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).




    Quote Originally Posted by oikram View Post
    3) Is that string a query string, in the same sense we call query strings when we talk about GET method usage?
    Still applies.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Location
    Kingdom of Saudi Arabia
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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'}.

  6. #6
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...


    Quote Originally Posted by modtup View Post
    1) What kind of data is Map?
    -> string data in key->value pair or mapping
    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.

    Quote Originally Posted by modtup View Post
    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.
    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.

    Quote Originally Posted by modtup View Post
    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'}.
    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?


    Regards,
    Márcio

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Location
    Kingdom of Saudi Arabia
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a newbie too...
    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.

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help. Ready to go.

    Yupii!!!

    Márcio


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •