Hi
Im quite new to this all and i know im doing something very wrong, but can’t seem to figure it out. Have tried to find tutorials and such but can’t seem to find much.
I’m trying to write an AJAX handler - I have quite a few small ajax things on this site im doing and its all getting rather messy so Im trying to write something that will handle all the ajax requests. Im going to post what i’ve written so far which is quite a bit. Hopefully you wont have to read through it all to point me in the right direction.
I think once im calling the ajax object inside the other other object it is (and i am) getting very confused about what this refers to.
Anyway here it is, and help or pointers will be greatly appreciated.
function AJAXRequest(url, data){
this.method = "GET";
this.url = url;
this.data = data;
this.onBeforeSend = function ()
this.onResponse = function()
this.onError = function()
{
Alert("There was an error processing your request. This may be due to an internet connectivity problem or otherwise you may be able to try again.");
}
this.reponse = "";
}
AJAXRequest.prototype.init = function(){
try{
this.req=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(oc){
this.req=null;
}
}
if(!this.req&&typeof XMLHttpRequest!="undefined"){
this.req = new XMLHttpRequest();
}
}
AJAXRequest.prototype.Send = function () {
this.init();
if(this.req == null) { Alert("Please upgrade your browser."); return false; }
this.req.onreadystatechange = function() {
if (this.req.readyState == 4)
{
if (this.req.status == 200)
{
this.response = this.responseText;
this.onResponse();
}
else
this.onError();
}
}
this.onBeforeSend();
if(this.method == "GET")
{
tableurl = this.url + "?formkey=" + get("formkey").value + "&random=" + make_random() + "&" + this.data;
this.req.open("get",tableurl , true);
try {
this.req.send(null);
} catch (ex) { this.onError(); }
}
else if(this.method == "POST")
{
this.req.open("POST", this.url, true);
this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.req.setRequestHeader("Content-length", this.data.length);
this.req.setRequestHeader("Connection", "close");
this.req.send(this.data);
}
}
function test(){
var list = new AJAXRequest("ajax/some_ajax.php");
list.onBeforeSend = function() { alert("About to send"); }
list.onResponse = function() { alert(this.response); }
list.Send();
}
Thanks!