-
March 26th, 2009, 04:23 AM
#1
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
-
March 26th, 2009, 06:28 AM
#2
Re: AJAX readyState = undefined
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 26th, 2009, 06:30 AM
#3
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.
-
March 26th, 2009, 07:31 AM
#4
Re: AJAX readyState = undefined
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(){}
.
-
March 26th, 2009, 07:41 AM
#5
Re: AJAX readyState = undefined
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;
}
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 26th, 2009, 07:59 AM
#6
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);
}
-
March 26th, 2009, 08:07 AM
#7
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.
-
March 26th, 2009, 08:14 AM
#8
Re: AJAX readyState = undefined
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 )
Last edited by TimothyH; March 26th, 2009 at 08:30 AM.
-
March 26th, 2009, 08:38 AM
#9
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.
-
April 8th, 2009, 08:18 AM
#10
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.
-
April 8th, 2009, 08:59 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|