# Other Programming > AJAX >  AJAX readyState = undefined

## TimothyH

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.



```
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); 
}
```

----------


## PeejAvery

[ moved thread ]

----------


## PeejAvery

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



```
alert(xhr.readyState);
```

----------


## TimothyH

> You are using _this_ which would be a subset of the function. You need to use the AJAX object to check for the readyState.
> 
> 
> 
> ```
> 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 

```
object.event = function(){}
```

.

----------


## PeejAvery

> 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.



```
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;
}
```




> i thought *this* referred to the "owner" of the function when the function is asigned like 
> 
> ```
> 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_.

----------


## TimothyH

Thanks for your help so far, though the solution you have given me still returns undefined  :Frown: .
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:


```
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); 
}
```

----------


## PeejAvery

Haha. I hate little mistakes...case sensitivity. Change _readystate_ to *readyState*.

----------


## TimothyH

> Haha. I hate little mistakes...case sensitivity. Change _readystate_ to *readyState*.


...  :Frown:  
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 )

----------


## PeejAvery

I have to apologize for that. It is correct for this situation. I was confused not thinking of it as an object.

----------


## TimothyH

*note*

I just found out that when using: 



```

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: 

```
xmlHttpRequestObject.readyState
```

I've not yet tested it in IE6 and below, anyone did ?.

----------


## PeejAvery

My personal rule is..._always use the actual object instead of this unless you are within your own custom made object_.

----------

