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);
}
Re: AJAX readyState = undefined
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);
Re: AJAX readyState = undefined
Quote:
Originally Posted by
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.
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(){}
.
Re: AJAX readyState = undefined
Quote:
Originally Posted by
TimothyH
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
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.
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);
}
Re: AJAX readyState = undefined
Haha. I hate little mistakes...case sensitivity. Change readystate to readyState.
Re: AJAX readyState = undefined
Quote:
Originally Posted by
PeejAvery
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 )
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.
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 ?.
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.