0

In most examples I found on the Internet , I see something like this :

ajaxRequest.onreadystatechange = function() {   
    if(ajaxRequest.readyState == 4) {
        document.myForm.time.value = ajaxRequest.responseText;
    }
}

ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);

How is it possible and how does this code work , when the "change state" property is checked BEFORE the open and send commands are executed ? I know it works...but how does the flow return back to "check the state status" after the "open" and "send" are executed.

I would appreciate any help

Many thanks in advance :-)

MM.
  • 2,653
  • 4
  • 26
  • 36
Portishead
  • 65
  • 2
  • 10

2 Answers2

0

Because onreadystatechange is an event, and that code won't get invoked until the ready state changes, which will occur at some point in the future when the request is complete.

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • OK , I understand that but the sequence is : First , check the state Then , Open and Send the command ...shouldnt it be : Open-->Send-->Check ??? – Portishead Feb 23 '12 at 12:44
  • No, because you want to define the code that will run when the request completes before initiating the request, otherwise the request won't know what to run (and won't run anything) when the ready state changes. – devdigital Feb 23 '12 at 12:53
  • Have a read of http://en.wikipedia.org/wiki/Event_%28computing%29 and event handlers. – devdigital Feb 23 '12 at 12:57
  • I see what you mean. First I'm declaring in some way what the triggering of the event should do , and then actually make the request. Am I right ? – Portishead Feb 23 '12 at 13:00
  • Thanks a lot my precious friend ! – Portishead Feb 23 '12 at 13:26
  • No problem. Don't forget to mark the answer as accepted if it solved your problem, or upvote it if you liked it, or do both. It makes people want to help you even more. – devdigital Feb 23 '12 at 13:28
  • Sorry, I'm new here.The only thing I'm allowed to do regarding the post is marking it as helpful.Is this OK? – Portishead Feb 23 '12 at 13:39
  • You can do that if you wish :) you should also be able to select the tick mark to accept the answer. – devdigital Feb 23 '12 at 13:43
0

This is exactly the flow of AJAX

Making a request

You have your shiny new XMLHttpRequest object; now take it for a spin. First, you need a JavaScript method that your Web page can call (like when a user types in text or selects an option from a menu). Then, you'll follow the same basic outline in almost all of your Ajax applications:

  1. Get whatever data you need from the Web form.
  2. Build the URL to connect to.
  3. Open a connection to the server.
  4. Set up a function for the server to run when it's done.
  5. Send the request.

Handling the response

Now you need to actually deal with the server's response. You really only need to know two things at this point:

* Don't do anything until the xmlHttp.readyState property is equal to 4.
* The server will stuff it's response into the xmlHttp.responseText property.
Sam Arul Raj T
  • 1,752
  • 17
  • 23
  • As your diagram implies: Browser sends the request-->Server processes the request and sends back data-->Browser processes the request delivered from the Server. But in the code...a status change is checked , even before anything is sent to the Server – Portishead Feb 23 '12 at 12:51
  • send(string),Sends the request off to the server. string: Only used for POST requests,,To POST data like an HTML form, add an HTTP header with setRequestHeader(). Specify the data you want to send in the send() method:,,xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("fname=Henry&lname=Ford"); more over check this link you will be good enough to know http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp – Sam Arul Raj T Feb 23 '12 at 15:04