views:

36

answers:

3

I tried the following code to send request to jsp page on a click of button. I checked on Httpfox but no request is going. I just used the whole of this code in the body of the html code. Am I doing some silly mistake. Kindly suggest..

<button type="button" onClick="handleButtonClick();">Click Me!</button>
<script type="text/javascript">




function handleButtonClick()
{
    // Declare the variables we'll be using
    var xmlHttp, handleRequestStateChange;

    // Define the function to be called when our AJAX request's state changes:
    handleRequestStateChange = function()
    {
        // Check to see if this state change was "request complete", and
        // there was no server error (404 Not Found, 500 Server Error, etc)
        if (xmlhttp.readyState==4 && xmlhttp.status==200) 
        {
            var substring=xmlHttp.responseText;
            // Do something with the text here
            alert(substring);
        }
    }

    xmlhttp = new XMLHttpRequest();
    xmlHttp.open("GET", "http://csce:8080/test/index.jsp?id=c6c684d9cc99476a7e7e853d77540ceb", true);
    xmlHttp.onreadystatechange = handleRequestStateChange;
    xmlHttp.send(null);
}
</script>
A: 

Since no request is being made, I am not convinced you can actually make requests to "http://csce:8080" as FireFox may not see that URL as being on the same subdomain (You cannot make Ajax requests for resources not on the same domain as the requestor).

Suppose you made the URL relative. Is a request even generated then? If so, that is likely your problem.

Eric Wendelin
Check tmont's answer first, it's probably correct.
Eric Wendelin
+3  A: 

Well, in JavaScript, variables are case-sensitive. You have xmlHttp and xmlhttp; those should be the same.

You've also got <pre><code> at the beginning of your <script> block, which is a JavaScript syntax error.

tmont
Good catch! Totally missed that when I checked it. That could very well be it.
Eric Wendelin
Thanks I didnt even realized I am using different case...
Judy
A: 

Quote: xmlhttp = new XMLHttpRequest();

Two things. First, you might want to use a more robust method of getting an XMLHttpRequest object. Second, javascript is case-sensitive; xmlhttp != xmlHttp

xmlHttp = (function (x,y,i) {
    if (x) return new x();
    for (i=0; i<y.length; y++) try { 
        return new ActiveXObject(y[i]);
    } catch (e) {}
})(
    window.XMLHttpRequest, 
    ['Msxml2.XMLHTTP','Microsoft.XMLHTTP']
);

Quote: http://csce:8080/test/ind...

Keep in mind that cross-domain xmlhttp is verboten. Unless you're serving from csce:8080, that ain't gonna work.

Fordi