Reputation: 217
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.
Upvotes: 0
Views: 524
Reputation: 1331
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);
}
Upvotes: 0
Reputation: 5496
try this
function funcOnChange() {
var index = document.detail.Class.selectedIndex;
var valueSelected = "Class="+document.detail.Class.options[index].text;
.....
.....
xhr.send(valueSelected);
}
Upvotes: 1