Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 331
Nazneen Avatar asked Feb 05 '26 08:02

Nazneen


2 Answers

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

like image 66
JSPDeveloper01 Avatar answered Feb 07 '26 21:02

JSPDeveloper01


Either declare httpRequest outside getStates() (as a global variable) or pass it to processRequest():

httpRequest.onreadystatechange = function() { processRequest(httpRequest); };

function processRequest(httpRequest) {
like image 38
Rodolphe Avatar answered Feb 07 '26 20:02

Rodolphe