CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2008
    Location
    The Netherlands
    Posts
    22

    AJAX readyState = undefined

    Hello Codegurus,

    The following piece of code returns a readystate that's "undefined".
    The alert appears 4 times in IE and in FF. so i reckon the readystate changes 4 times ?; but it's always "undefined".
    Could someone tell me what i'm doing wrong?, Thanks.

    EDIT: Sorry, just noticed the AJAX forum. feel free to move this topic.

    Code:
    function xmlHttpRequest(){
    if (window.XMLHttpRequest)
      {
      return new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    else
      {
      alert("XMLHTTP not supported.");
      return null;
      }
    }
    
    function ajaxFunction(){
     
      var xhr = xmlHttpRequest();
      xhr.onreadystatechange = function(){
       
        alert(this.readystate);
    
      }
      xhr.open("GET","url",true);
      xhr.send(null); 
    }
    Last edited by TimothyH; March 26th, 2009 at 04:29 AM. Reason: Wrong category

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: AJAX readyState = undefined

    [ moved thread ]
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: AJAX readyState = undefined

    You are using this which would be a subset of the function. You need to use the AJAX object to check for the readyState.

    Code:
    alert(xhr.readyState);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Sep 2008
    Location
    The Netherlands
    Posts
    22

    Re: AJAX readyState = undefined

    Quote Originally Posted by PeejAvery View Post
    You are using this which would be a subset of the function. You need to use the AJAX object to check for the readyState.

    Code:
    alert(xhr.readyState);


    Thanks for your reply, but i have tried this already and i still get an "undefined"... i thought this referred to the "owner" of the function when the function is asigned like
    Code:
    object.event = function(){}
    .

  5. #5
    Join Date
    May 2002
    Posts
    10,943

    Re: AJAX readyState = undefined

    Quote Originally Posted by TimothyH View Post
    Thanks for your reply, but i have tried this already and i still get an "undefined"...
    I missed it at first, but took a closer look this time. Your problem is two-fold. 1. I already mentioned about this being invalid in this context. 2. You are forgetting the other two possible protocols for AJAX implementation. Use the following instead of your xmlHttpRequest() function.

    Code:
    function xmlHttpRequest() {
      var xmlHTTP;
      try {xmlHTTP = new XMLHttpRequest();}
      catch(e) {
        try {xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");}
        catch(e) {
          try {xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");}
          catch(e) {
            alert("Your browser does not support AJAX!");
            return false;
          }
        }
      }
      return xmlHTTP;
    }
    Quote Originally Posted by TimothyH View Post
    i thought this referred to the "owner" of the function when the function is asigned like
    Code:
    object.event = function(){}
    .
    this refers to the owner even if it isn't declared like that. Either way, this is not correct in your original code, it still should be xhr.readyState.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Sep 2008
    Location
    The Netherlands
    Posts
    22

    Re: AJAX readyState = undefined

    Thanks for your help so far, though the solution you have given me still returns undefined .
    I'ts weird because i've used exactly the same code in another section of my application and it does not return an error there. And just because i'm curious and want to learn: in what type of situation can i use the this correctly?.

    P.S.

    This is what my code looks like now:
    Code:
    function xmlHttpRequest() {
      var xmlHTTP;
      try {xmlHTTP = new XMLHttpRequest();}
      catch(e) {
        try {xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");}
        catch(e) {
          try {xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");}
          catch(e) {
            alert("Your browser does not support AJAX!");
            return false;
          }
        }
      }
      return xmlHTTP;
    }
    
    function exampleFunction(){
     
      var xhr = xmlHttpRequest();
      xhr.onreadystatechange = function(){
       
        alert(xhr.readystate);
    
      }
      xhr.open("GET","url",true);
      xhr.send(null); 
    }

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: AJAX readyState = undefined

    Haha. I hate little mistakes...case sensitivity. Change readystate to readyState.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  8. #8
    Join Date
    Sep 2008
    Location
    The Netherlands
    Posts
    22

    Re: AJAX readyState = undefined

    Quote Originally Posted by PeejAvery View Post
    Haha. I hate little mistakes...case sensitivity. Change readystate to readyState.
    ...
    Thanks for the help. it doesn't help that when assigning an event you have to use all lowercase.


    EDIT: i have one more question. In what type of context can i use this?
    ( if this context is wrong )
    Last edited by TimothyH; March 26th, 2009 at 08:30 AM.

  9. #9
    Join Date
    May 2002
    Posts
    10,943

    Re: AJAX readyState = undefined

    I have to apologize for that. It is correct for this situation. I was confused not thinking of it as an object.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  10. #10
    Join Date
    Sep 2008
    Location
    The Netherlands
    Posts
    22

    Re: AJAX readyState = undefined

    note

    I just found out that when using:

    Code:
    xmlHttpRequestObject.onreadystatechange = function(){
    
     if(this.readyState==value){
    
      function();
     
     }
    
    }


    in the situation illustrated above; this is not supported by FireFox version 2.0.0.14 ( and below, i reckon )
    so you're obligated to use:
    Code:
    xmlHttpRequestObject.readyState
    I've not yet tested it in IE6 and below, anyone did ?.
    Last edited by TimothyH; April 8th, 2009 at 08:25 AM.

  11. #11
    Join Date
    May 2002
    Posts
    10,943

    Re: AJAX readyState = undefined

    My personal rule is...always use the actual object instead of this unless you are within your own custom made object.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured