OO Help In JavaScript

T

trentula

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://www.google.com/');

alert(asd.getResponse());

asd.foo();
 
R

RobG

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.

Posting a few hundred lines of code with "this doesn't work" wont get
you very far. If you just want a functional AJAX library, try:

<URL: http://www.ajaxtoolbox.com/ >


If you are doing this as a learning exercise, then you need to at least
point to where the code is failing and what your error messages are.
 
T

trentula

Sorry for taking a code dump on you. The bit of code that is giving me
problems is in the this.handler method. More specifically, it's any
line that makes reference to this.requestObject, telling me that the
property is not an actualy property of that variable. From what I could
gleam, this.requestObject is being reset between create() and this. I'm
guessing there's some fundamental flaw in my logic, and, as before, any
insight will be greatly appreciated.

this.handler = function()
{
try
{
// When this check is removed, I get the crash. Otherwise, it just
trips the error message.
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);
}
};
 
R

RobG

Sorry for taking a code dump on you. The bit of code that is giving me
problems is in the this.handler method. More specifically, it's any
line that makes reference to this.requestObject, telling me that the
property is not an actualy property of that variable. From what I could
gleam, this.requestObject is being reset between create() and this. I'm
guessing there's some fundamental flaw in my logic, and, as before, any
insight will be greatly appreciated.

There is an article here <URL: http://blog.livollmers.net/?p=17 > that
discusses the issue. The author uses Prototype.js, but the theory is
there. The basic problem is that when the callback occurs, the value
of the this keyword is not what you expect.

Or use the link I provided to see how AjaxToolbox solves it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top