Thread: Ajax readyState

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Ajax readyState




    Code:
    if (xmlhttp.readyState==3 && xmlhttp.status==200) {
     document.getElementById("content").innerHTML="...";
    }
    Only readyState == 4 works!.

    I also tried
    Code:
    while (xmlhttp.readyState == 3)
    I'd like to print "..." while processing the data ..

    Thx. Kr, C.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,123
    Rep Power
    119
    The fastest way; to do this; would be something like this:

    Code:
    function ajaxRequest()
    {
    // define xmlhttp and/or other actions here
    document.getElementById("content").innerHTML="...";
     if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      // handle responseText here
     }
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0
    Great!
    Thx, web_loone08.

    There is a problem and it's strange.

    If I define my XMLHttpRequest variable inside the function, the code:
    Code:
    document.getElementById("content").innerHTML="...";
    keeps running and (xmlhttp.readyState==4 && xmlhttp.status==200) never becomes true.

    Otherwise OK!

    Good:
    Code:
    var xmlhttp;
    function ajaxRequest()
    {
    // initialize xmlhttp and/or other actions here
    document.getElementById("content").innerHTML="...";
     if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      // handle responseText here
     }
    }
    Bad:
    Code:
    function ajaxRequest()
    {
    // define xmlhttp and/or other actions here
    document.getElementById("content").innerHTML="...";
     if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      // handle responseText here
     }
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0
    You can't define your xmlhttp variable in the same function that you use to detect changes in your readyState. xmlhttp is the owner of that readyState, if you redefine xmlhttp in that function you lose the original xmlhttp.

    Your "ajaxRequest" function will fire every time the readyState changes, which is why you have to check to be sure that readyState is 4 so you know the request is done. As an aside, realize that readyStates 1, 2 and 3 are poorly implemented in most browsers and are not really to be reliably used for anything. It's not uncommon for a browser to skip 2 or 3 and go straight to 4 in my experience.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0
    Yes, I am not using those states anymore cauz as u said almost nobody uses them.

    When I load a simple page with xmlhttpR. and I put something before (is state 4) then it works, but in all other cases, it doesn't. Especially when I send post/get message to an PHP file to do some db stuff.. rare!!


    thx, anyway...

IMN logo majestic logo threadwatch logo seochat tools logo