Click to See Complete Forum and Search --> : AJAX readyState = undefined


TimothyH
March 26th, 2009, 04:23 AM
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
March 26th, 2009, 06:28 AM
[ moved thread ]

PeejAvery
March 26th, 2009, 06:30 AM
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
March 26th, 2009, 07:31 AM
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
March 26th, 2009, 07:41 AM
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
March 26th, 2009, 07:59 AM
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:
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
March 26th, 2009, 08:07 AM
Haha. I hate little mistakes...case sensitivity. Change readystate to readyState.

TimothyH
March 26th, 2009, 08:14 AM
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 )

PeejAvery
March 26th, 2009, 08:38 AM
I have to apologize for that. It is correct for this situation. I was confused not thinking of it as an object.

TimothyH
April 8th, 2009, 08:18 AM
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
April 8th, 2009, 08:59 AM
My personal rule is...always use the actual object instead of this unless you are within your own custom made object.