Search code examples
javascriptajaxspringjspjstl

Ajax request not passed to controller


I send an AJAX request to my controller. This development done in JSP and Spring environment. SimpleFormController is overridden by the controller I'm using.

Using JavaScript I create the object and send the request. this request is not getting passed to controller.

JavaScript code which send the request.

function getStates(){
    var httpRequest;
    var country = document.getElementById('countryName');
    alert(country);
    var url = '/developer/register.htm';

    url = url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true');

     if (window.ActiveXObject)
     {
         httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
     }
     else if (window.XMLHttpRequest)
     {
         httpRequest = new XMLHttpRequest();
     }

     httpRequest.open("GET", url, true);
     httpRequest.onreadystatechange = function() {processRequest(); };
     httpRequest.send(null);
}

function processRequest() {
    if(httpRequest.readyState == 4){
        alert("inside ready state");
        var response = http.responseText;
        document.getElementById('states').innerHTML = response;
    }
}

Variable url is given in the way how I have mentioned in dispatcher servlet.

The browser shows an error in processRequest() function telling that httpRequest is not defined. but, I don't get this error in the previous lines. I use this object in getStates() function.


Solution

  • It is because variable httpRequest should be defined outside getStates() function. Otherwise processRequest() cannot see it.