Jump to content

help with an AJAX class


bennyboywonder

Recommended Posts

I am currently developing an ajax based website. Currently all the xmlHttp calls are handled by one function and a couple of big SWITCH statements inside it, which I think is quite an ugly way of doing it. Am trying to rewrite it with a class, but it doesn't seem to work. Here's the class

function htRequest(page, query) {
this.constructUrl = function() {
	if(this.query) this.url = this.page;
	else this.url = this.page + "?" + this.query;
}
this.send = function() {
	this.constructUrl();
	this.xhReq = createXMLHttpRequest();
	this.xhReq.open("GET", this.url, true);
	this.xhReq.onreadystatechange = this.onLoad;
	this.xhReq.send(NULL);
	this.whileLoading();
}
this.page = page;
this.query = query;
this.constructUrl();
}

 

then when I want to use it, the idea is I do something like this,

htr = new htRequest("xmlHTTP.php");
htr.whileLoading = function() {
// Loading code here
}
htr.onLoad = function() {
// What to do with the date when finished loading it here
}
htr.send();

then when I need to reuse the same call, but with different input data

htr.query = "string1=blah&string2=blah";
htr.send();

 

This does not seem to work however, and I can't figure out why. My knowledge of javascript is a bit gappy, so any help would be appreciated.

Link to comment
Share on other sites

Ok well I have solved my original problem, but now I notice that when I try to call this.onLoad(); inside the onreadystatechange handler it doesn't work. Anyone have any clues why?

 

function htRequest(page, query)
{
    this.constructUrl = function() {
    {
        if(!this.query) this.url = this.page;
        else this.url = this.page + "?" + this.query;
    }

    }
    this.send = function()
    {
        this.constructUrl();
        http = createXMLHttpRequest();
        http.open("GET", this.url, true);
http.onreadystatechange = function()
        {
            
            if (http.readyState != 4)
            {
                return;
            } 
            
            if (http.status != 200)
            {
                return;
            }
            this.onLoad();
        }
http.send(null);
this.whileLoading();
    }
    this.page = page;
    this.query = query;
    this.constructUrl();
}

htr = new htRequest("xmlHTTP.php");

htr.whileLoading = function()
{
    alert("loading");
}

htr.onLoad = function()
{
    alert("loaded");
}

htr.send();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.