tags:

views:

56

answers:

4

So we have this:

$.post('testeUpdate.php', 'someValue'
        function(dadosResposta) {
            $('#ns-nome').val(dadosResposta.nsName);
            $('#ns-endereco').val(dadosResposta.nsAddress);
        },
"json");

From client to server: Is sends 'sameValue' to testeUpdate.php using POST.

On success, it receives the data returned by the server side script and do something with that on the client side. Correct?

There is something between the Server Side script and this Client Side script that I'm not getting.

Question: How does dadosResposta gets filled with the data returned from the serverside script ? How does it knows? How does it work? Perhaps the question may also be posed: how do jquery knows when something is "on success" ?

Or trying on another way: The server side executes the script, returns some encoded data in json. Until here ok. After this, what happens? How do we jump into the client side part again?

[update] Trying again: I know that: dadosResposta contains whatever the server-side script returns my question is, HOW does it knows about that? How does he do that? [/update]

Thanks a lot, MEM

+1  A: 

$.post takes a callback function. That callback takes one argument, which is the data returned from the server. So, in your case, dadosResposta is the data returned from the server.

It knows whether it was successful or not because of the status code in the HTTP header returned (200 OK in this case). You can include a second argument in your callback which will contain the status.

EDIT: It knows because that's how jQuery works. jQuery gets the response, and passes it as a parameter to your callback. If you want to know more about the internals, read up on XMLHttpRequest.

Skilldrick
thanks a lot. please note my [update] part on the initial question. And thanks for the additional header info.
MEM
@MEM see my edit.
Skilldrick
"It knows because that's how jQuery works"... Naaa. That's not an answer is it? :p However, the XMLHttpRequest that it uses may seem to be the answer. I still don't know how does jquery does that. I know that somehow, it will use XMLHttpRequest, but no more then that. I guess I cannot have my answer here. And if I do, I will probably not get it at once.Thanks in advance. :)
MEM
I don't mean to offend, but I feel like I have a pretty good explanation of what's going on just below.
Ryan Kinal
@MEM The reason I said "because that's how it works" is because you were asking "how it knows". That's a bit like asking how JavaScript "knows" that you want `x` to be `3` when you say `x = 3`. It knows because that's how it works; that's how JavaScript is defined. Maybe I should have said "it knows because that's how the function is defined".
Skilldrick
@Skilldrick :) Even that would not be an answer... I would then ask: "How is the function defined, so that I could understand, how does it work?" :) Common, this is not a question of having patience or not. ;) I still have doubt's with Ryan Kinal answer, like: "And how XMLHttpRequest knows that? But that was not what I asked, so I keeped that doubt to myself. :)
MEM
@MEM You're delving deeper and deeper into the technology with each question! Once you know how XHR works you'll want to know how HTTP works, and then you'll want to know how TCP/IP works (after that it's turtles all the way down). The question, however, was how jQuery does it, and it does it with XHR :)
Skilldrick
lol I don't like turtles and I doubt that they have the strength to support the all world. ;) But who knows... :)Still, :p the question however is "how jQuery does it" and it does it in a way that we seem to not know, still, it USES XHR for doing that.Analogous, we can know that the carpenter USES a saw for doing is job, but that doesn't give us the answer about HOW have he done that magnificent table. On my way to turtles,Cheers ;)
MEM
@MEM Hehe happy to help :)
Skilldrick
+1  A: 

The parameter in the callback function (dadosResposta in this case) contains whatever the server-side script returns. For example, if the server side script was just returning the word "completed" in plain text, dadosResposta would be the string "completed".

In this particular case, the data is JSON, which allows the data to be better organized so that specific fields can be accessed as in dadosResposta.nsName.

DLH
thanks a lot. please note my [update] part on the initial question.
MEM
Sorry, I'm not really sure what you're asking. Are you asking how it knows to format it as JSON? The last parameter of `$.post()` is specified as "json", and I assume the Content-Type header of the server-side script would be `application/json`.
DLH
np DLH. It was a hard one to make anyway and I'm sorry for my poor capacities for clarification. :s Still, all ended well. :)
MEM
+1  A: 

jQuery knows the call was succesfull when the request returns a http status code of 200. If the server fails to process the request, an error code (ie 500) should be returned.

zwip
thanks a lot. please note my [update] part on the initial question.
MEM
+5  A: 

The jQuery .post function uses an XMLHttpRequest object to access the URL you passed to it. The server responds with either a 200 OK (success) or some other response (presumably failure).

On success, the XMLHttpRequest object has its responseText (or responseXML) property set to the data returned by the server. The behind-the-scenes jQuery function that handles the response then looks at your dataType argument to determine what to do with the returned data. In the case of your example, it attempts to parse it into an object.

Assuming the response is a valid JSON string, it then passes that newly created object to your callback function as its first (and only) argument, which you've named dadosResposta.

That is how jQuery gets your data into dadosResposta.

Edit: Here's some code that's probably vaguely akin to what's going on:

$.post = function([arguments])
{
    var xhr = new XMLHttpRequest(); // not cross browser compatible, but good enough for demonstration
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState === 4 && xhr.status === 200)
        {
            switch (your_dataType)  // check your data type
            {
                case 'json':
                    your_callback(json_decode(xhr.responseText));    // call your function with JSON object
                    break;
                case 'xml':
                    your_callback(xhr.responseXML);    // call your function with XML
                case ... (etc)
            }
        }
    }

    xhr.open('POST', your_URL, true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.send();
}
Ryan Kinal
What part in my code have made you make this assumption: "In the case of your example, it attempts to parse it into an object." ? Thanks a lot.
MEM
Your last argument to `$.post` is `json`. This means the function will assume the server returns a JSON string, which jQuery will then parse into an object (assuming it's a valid JSON string).
Ryan Kinal
+1 You have more patience than me :)
Skilldrick
@Ryan Kinal - thanks for the details provided. It has help me to start to understand what's going on here. :D
MEM
Always glad to help! (Also, here's some more info on the XMLHttpRequest object: http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx)
Ryan Kinal