0

I am getting null when trying to receive parameter from JavaScript. I have gone through few post, but could not figure out where i am making the mistake in my code.

Below is code from where i am sending request:

function funcOnChange() {
    var index = document.detail.Class.selectedIndex;
    var valueSelected = document.detail.Class.options[index].value;

    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;
            alert("Alert Dialog! Gaurav");
        }
    }
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://localhost:8080/ReportFetcher/FormHandler', true);
    xhr.send(valueSelected);
}

I am getting the valueselected from the following piece of code and valueselected's is correct:

<select name="Class" onchange="funcOnChange()">
            <option value="None">None</option>
            <option value="FIRST">FIRST</option>
            <option value="SECOND">SECOND</option>
            <option value="THIRD">THIRD</option>
            <option value="FOURTH">FOURTH</option>
            <option value="FIFTH">FIFTH</option>
            <option value="SIXTH">SIXTH</option>
            <option value="SEVENTH">SEVENTH</option>
            <option value="EIGHTH">EIGHTH</option>
            <option value="NINTH">NINTH</option>
            <option value="TENTH">TENTH</option>
        </select><br>

I am receiving a callback on onPost() of FormHandler.java

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    log.info("IN form doPost");
    String selectedClass = request.getParameter("Class");
    log.info(selectedClass);

}

Problem: selectedClass is null here.

Suggest where i am making mistake.

Kanishka Gupta
  • 217
  • 2
  • 6
  • 17
  • I think there is some issue with key-value mapping, but could not find out where problem is occurring. Also, i tried passing value using GET request and i got the parameter. – Kanishka Gupta May 15 '13 at 11:28

2 Answers2

1

try this

function funcOnChange() {
    var index = document.detail.Class.selectedIndex;
    var valueSelected = "Class="+document.detail.Class.options[index].text;

    .....
    .....

    xhr.send(valueSelected);
}
NPKR
  • 5,368
  • 4
  • 31
  • 48
  • This worked for me, is there exist a better way to define key for the valueSelected . – Kanishka Gupta May 15 '13 at 11:31
  • I think no, key will be accessed in Servlet so manually we need to sepicify some Key – NPKR May 15 '13 at 11:32
  • No, i think there was some compilation or refresh issue. But this way is not working. I restarted the server and compiled it again. So I was not getting the parameter. So please suggest some other way out. – Kanishka Gupta May 15 '13 at 11:37
  • If you want to read the data without key value try HTTPServletRequest getReader interface. Though I will recommend not to use it. An example is provided in this question http://stackoverflow.com/questions/3831680/httpservletrequest-get-post-data – vdua May 15 '13 at 11:44
0

If you see through any proxy/ HTTP monitor tool ( I use Charles) your requests and responses you will see that you are not sending the request as key value pairs ( in simple terms you are not sending Class=value) but you are sending only the value of Class attribute as string. (i.e. Third if you select the option Third in the select box). You need to send the FormData if you want to read data on server as key value pairs.

function funcOnChange() {
    var index = document.detail.Class.selectedIndex;
    var valueSelected = document.detail.Class.options[index].value;

    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;
            alert("Alert Dialog! Gaurav");
        }
    }
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://fiddle.jshell.net/', true);    
    var form = new FormData();
    form.append("Class",valueSelected)
    xhr.send(form);
}
vdua
  • 1,281
  • 1
  • 14
  • 24