T
trent.bradley
I've just recently gotten around to JavaScript, especially its brand of
OOP, and for the life of me can't figure out why this code doesn't
work. Any help will be greatly appreciated.
function AJAX()
{
this.response = null;
this.responseXML = null;
this.responseHeaders = null;
this.requestObject = null;
this.statusNumber = null;
this.statusText = null;
this.abort = function()
{
if(this.requestObject)
{
this.requestObject.abort();
}
};
this.create = function()
{
if(window.XMLHttpRequest)
{
this.requestObject = new XMLHttpRequest();
}
else
{
if(window.ActiveXObject)
{
try
{
this.requestObject = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e)
{
try
{
this.requestObject = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e)
{
alert(e);
}
}
}
}
};
this.GET = function(url)
{
try
{
this.abort();
this.open('GET', url);
this.setHandler(this.handler);
this.send(null);
}
catch(e)
{
alert(e);
}
};
this.getResponseHeaders = function()
{
var ret = null;
if(this.requestObject)
{
ret = this.requestObject.getAllResponseHeaders();
}
return ret;
};
this.getResponse = function()
{
return this.response;
};
this.getResponseHeader = function(header)
{
var ret = null;
if(this.requestObject)
{
ret = this.req_obj.getResponseHeader(header);
}
return ret;
};
this.getResponseText = function()
{
return this.response;
};
this.getResponseXML = function()
{
return this.responseXML;
};
this.getState = function()
{
var ret = null;
if(this.requestObject)
{
ret = this.requestObject.readyState;
}
return ret;
};
this.getStatus = function()
{
return this.statusNumber;
};
this.getStatusText = function()
{
return this.statusText;
};
this.handler = function()
{
/*
0: Uninitialized
1: Loading
2: Loaded
3: Interactive
4: Finished
*/
try
{
//if(this.requestObject)
//{
this.statusNumber = this.requestObject.status;
this.statusText = this.requestObject.statusText;
if(this.requestObject.readyState == 4)
{
if(this.requestObject.state == 200)
{
this.response = this.requestObject.responseText;
this.responseXML = this.requestObject.responseXML;
this.responseHeaders = this.getAllResponseHeaders();
}
else
{
alert('There is a problem with the last request.');
}
}
//}
//else
//{
//alert('There is a problem with the XMLHTTPRequest Object.');
//}
}
catch(e)
{
alert(e);
}
};
this.open = function(method,
url,
async,
username,
password)
{
if(this.requestObject)
{
if(password)
{
this.requestObject.open(method, url, async, username, password);
}
else
{
if(username)
{
this.requestObject.open(method, url, async, username);
}
else
{
if(async)
{
this.requestObject.open(method, url, async);
}
else
{
this.requestObject.open(method, url);
}
}
}
}
};
this.POST = function(url,
data)
{
try
{
this.abort();
this.open('POST', url);
this.setHandler(this.handler);
this.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
this.send(data);
}
catch(e)
{
alert(e);
}
};
this.send = function(data)
{
if(this.requestObject)
{
this.requestObject.send(data);
}
};
this.setHandler = function(handler)
{
if(this.requestObject)
{
this.requestObject.onreadystatechange = handler;
}
};
this.setRequestHeader = function(label,
value)
{
if(this.requestObject)
{
this.requestObject.setRequestHeader(label, value);
}
};
this.foo = function()
{
alert(this);
};
}
var asd = new AJAX();
asd.create();
asd.GET('http://localhost/');
alert(asd.getResponse());
asd.foo();
OOP, and for the life of me can't figure out why this code doesn't
work. Any help will be greatly appreciated.
function AJAX()
{
this.response = null;
this.responseXML = null;
this.responseHeaders = null;
this.requestObject = null;
this.statusNumber = null;
this.statusText = null;
this.abort = function()
{
if(this.requestObject)
{
this.requestObject.abort();
}
};
this.create = function()
{
if(window.XMLHttpRequest)
{
this.requestObject = new XMLHttpRequest();
}
else
{
if(window.ActiveXObject)
{
try
{
this.requestObject = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e)
{
try
{
this.requestObject = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e)
{
alert(e);
}
}
}
}
};
this.GET = function(url)
{
try
{
this.abort();
this.open('GET', url);
this.setHandler(this.handler);
this.send(null);
}
catch(e)
{
alert(e);
}
};
this.getResponseHeaders = function()
{
var ret = null;
if(this.requestObject)
{
ret = this.requestObject.getAllResponseHeaders();
}
return ret;
};
this.getResponse = function()
{
return this.response;
};
this.getResponseHeader = function(header)
{
var ret = null;
if(this.requestObject)
{
ret = this.req_obj.getResponseHeader(header);
}
return ret;
};
this.getResponseText = function()
{
return this.response;
};
this.getResponseXML = function()
{
return this.responseXML;
};
this.getState = function()
{
var ret = null;
if(this.requestObject)
{
ret = this.requestObject.readyState;
}
return ret;
};
this.getStatus = function()
{
return this.statusNumber;
};
this.getStatusText = function()
{
return this.statusText;
};
this.handler = function()
{
/*
0: Uninitialized
1: Loading
2: Loaded
3: Interactive
4: Finished
*/
try
{
//if(this.requestObject)
//{
this.statusNumber = this.requestObject.status;
this.statusText = this.requestObject.statusText;
if(this.requestObject.readyState == 4)
{
if(this.requestObject.state == 200)
{
this.response = this.requestObject.responseText;
this.responseXML = this.requestObject.responseXML;
this.responseHeaders = this.getAllResponseHeaders();
}
else
{
alert('There is a problem with the last request.');
}
}
//}
//else
//{
//alert('There is a problem with the XMLHTTPRequest Object.');
//}
}
catch(e)
{
alert(e);
}
};
this.open = function(method,
url,
async,
username,
password)
{
if(this.requestObject)
{
if(password)
{
this.requestObject.open(method, url, async, username, password);
}
else
{
if(username)
{
this.requestObject.open(method, url, async, username);
}
else
{
if(async)
{
this.requestObject.open(method, url, async);
}
else
{
this.requestObject.open(method, url);
}
}
}
}
};
this.POST = function(url,
data)
{
try
{
this.abort();
this.open('POST', url);
this.setHandler(this.handler);
this.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
this.send(data);
}
catch(e)
{
alert(e);
}
};
this.send = function(data)
{
if(this.requestObject)
{
this.requestObject.send(data);
}
};
this.setHandler = function(handler)
{
if(this.requestObject)
{
this.requestObject.onreadystatechange = handler;
}
};
this.setRequestHeader = function(label,
value)
{
if(this.requestObject)
{
this.requestObject.setRequestHeader(label, value);
}
};
this.foo = function()
{
alert(this);
};
}
var asd = new AJAX();
asd.create();
asd.GET('http://localhost/');
alert(asd.getResponse());
asd.foo();