Ajax Class >> Not passing responseText
Posted: Fri Feb 29, 2008 6:58 pm
Hello, world!
Here I am, trying to make an Ajax class (just a little something to make my life easier), and lo and behold, it doesn't work. I've been testing, and got it down to this:
There is a button that alerts the value of this.rText. It alerts "undefined".
It's supposed to pass the responseText to the class variable rText, but it doesn't.
Have been testing, and figured it probably would be a quick fix for you Javascript gurus.
Thanks!
Here I am, trying to make an Ajax class (just a little something to make my life easier), and lo and behold, it doesn't work. I've been testing, and got it down to this:
Code: Select all
function CAjax(){
this.load = false;
this.rText = false;
this.rXML = false;
this.rHTML = false;
this.getAjax = function (){
var ajx = false;
try {
ajx = new XMLHttpRequest();
}catch (e){
try {
ajx = new ActiveXObject('Msxml2.XMLHTTP');
}catch (e){
ajx = new ActiveXObject('Microsoft.XMLHTTP');
}
}
return ajx;
}
this.connect = function (url, method, fields){
method = method ? method : 'get';
method = method.toLowerCase();
var ajx = this.getAjax();
if (ajx){
if (method=='get'){
ajx.open('GET', url+'?'+fields, true);
ajx.onreadystatechange = function (){
this.load = ajx.readyState;
if (ajx.readyState==4){
this.rText = ajx.responseText;//This is the line
}
}
ajx.send(null);
}
}else{
return false;
}
}
}
var ajax = new CAjax();
ajax.connect('url', 'get', false);It's supposed to pass the responseText to the class variable rText, but it doesn't.
Have been testing, and figured it probably would be a quick fix for you Javascript gurus.
Thanks!