473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

OO Help In JavaScript

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();

Jan 18 '07 #1
3 1340

tr******@gmail.com wrote:
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.
--
Rob

Jan 18 '07 #2
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);
}
};

Jan 19 '07 #3

tr******@gmail.com wrote:
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.
--
Rob

Jan 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.