Here's how I call this code ... it's my wrapper for Ajax:

var ajax = new XMLHttp();
ajax.get('myscript.php?variable=value');

When I run that, I get the error in Firefox's Error Console that ajax.get is not a function. So I've checked this code ten times and I see nothing wrong. It's almost copied exactly from an example I saw online, and that example works (so says the author, it's from About.com). Here's all the pertinent javascript code:

function XMLHttp() {
    if (window.XMLHttpRequest) { // If we have a Gecko browswer?
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE is more difficult ...
        var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp", "MSXML2.XmlHttp.3.0",  "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.5.0"];
        for (var i = avers.length -1; i >= 0; i--) {
            try {
                httpObj = new ActiveXObject(avers[i]);
                return httpObj;
            } catch(e) {}
        }
    }    
    
    // And what if the the browser sucks?
    alert("Your broswer is too old. Consider upgrading!");
}

XMLHttp.prototype.get = function(url, element) {
    this.open('GET', url, true);
    this.onreadystatechange = function() {
        this.processRequest(element);
    }
    this.send(null);
}

XMLHttp.prototype.processRequest = function(element) {
    if (this.readyState == 4 && this.status == 200) {
        findElement(element).innerHTML = this.responseText;
    }
}

function findElement(target) {
    var targetElem = document.getElementById(target);
    
    if(!targetElem) {
        targetElem = getElementsByClassName(document, "*", target);
    }
    
    return targetElem;
}

The XMLHttp() function works fine, as I can use ajax.open and ajax.onreadystatechange. But I can't use ajax.get ... I've tried calling the XMLHttp() function without "new" before it ...

I'm lost ... what's wrong with that code?

Recommended Answers

All 2 Replies

The only problem I can see on the face of it is that you defined the get function as taking two parameters and you're only calling it with one parameter...

That doesn't usually cause a problem (Javascript is quite forgiving), but it's worth checking.

I have tried that actually, I saw that last night. I plugged in the ID of the <div> I wanted to update, using both single, double, and no quotes. To no avail. I still get "ajax.get is not a function".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.