[thelist] Ajax error? Not a function?

Chris Ditty cditty at gmail.com
Mon Oct 9 16:57:43 CDT 2006


Didn't look like this sent the first time.  If it is a duplicate, please
delete.

Can someone please tell me what I am doing wrong here?  I picked up the Sams
book "Ajax in 10 minutes" and I am having some problems with the code.  I am
getting an error on the myRequest.open line.  It says that myRequest.open is
not a function.

<script language="javascript">
function getXMLHTTPRequest(){
    var request = false;
    if(window.XMLHTTPRequest){
        request = new getXMLHTTPRequest(); /* Firefox and other good
browsers */
    }else{
        if(window.ActiveXObject){
            try{
                request = new ActiveXObject("Msm12.XMLHTTP"); /* some
versions of IE */
                }
            catch(err1){
                try{
                    request = new ActiveXObject("Microsoft.XMLHTTP"); /*
other versions of IE */
                    }
                catch(err2){
                    request = false;
                }
            }
        }
    }
    return request;
}

var myRequest = getXMLHTTPRequest();

function callAjax(){
    // declare a variable to hold some information to parse to the server.
    var lastname = 'Smith';

    // build the URL of the server script we wish to call
    var url = "myserverscript.php?surname=" + lastname;

    // generate a random number
    var myRandom = parseInt( Math.random()*999999999);

    // ask our XMLHTTPRequest object to open a server connection
    myRequest.open("GET", url + "&rand=" + myRandom, true);

    // prepare a function responseAjax() to run when the response has
arrived
    myRequest.onreadystatechange = responseAjax;

    // and finally send the request
    myRequest.send(null);
}

function responseAjax(){
    // we are only interested in a readyState of 4, ie... loaded
    if(myRequest.readyState == 4){
        // if server HTTP response is "OK"
        if(myRequest.status == 200){
            alert("Yippie.  It worked");
        }else{
            // issue an error message for any other HTTP response
            alert("An error has ocurred: " + myRequest.statusTest);
        }
    }
}
</script>
</head>

<body>
<form name="form1">
Name: <input type="text" name="myname" onblur="callAjax()" /><br />
Tel: <input type="text" name="telco" /><br />
<input type="submit" />
</form>



More information about the thelist mailing list