Judy
Judy

Reputation: 1553

XMLHTTPrequest request not working

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>

Upvotes: 2

Views: 3962

Answers (3)

Fordi
Fordi

Reputation: 2886

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.

Upvotes: 1

tmont
tmont

Reputation: 2630

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.

Upvotes: 5

Eric Wendelin
Eric Wendelin

Reputation: 44379

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.

Upvotes: 1

Related Questions