I am trying to pass these two variables to a servlet, but get an error stating that sessionid is a null string.
var sessionid = document.getElementById("Session-select").value;
var attendeeid = document.getElementById("Attendee-select").value;
console.log(attendeeid + " " + sessionid);
$.ajax({
url: 'http://localhost:8180/Project_2_Highfield/registrations',
contentType: 'application/json',
method: 'PUT',
data: {sessionid: sessionid, attendeeid: attendeeid},
dataType: 'json',
success: function (jsondata) {
console.log(JSON.stringify(jsondata));
document.getElementById("output").innerHTML = "";
var success = jsondata['success'];
$("#output").append("<p>"+ "Success: " + success +"</p>");
}});
The error is as follows:
java.lang.NumberFormatException: Cannot parse null string
I tried adding a console log to ensure the variables weren’t null; they aren’t, yet one still gets sent as null in the ajax query.
Here is the HTML related to this part.
<body>
<fieldset >
<legend>Session Management</legend>
<label for="Sessions">Select Session:</label>
<select name="Session-select" id="Session-select"> </select>
<p></p>
<label for="Attendees">Select Attendee:</label>
<select name="Attendee-Select" id="Attendee-select"> </select>
<p></p>
<input type="button" value="List Registrations" onclick="list_session_attendees();">
<input type="button" value="Update Registration" onclick="update_registration();">
<input type="button" value="Cancel Registration" onclick="cancel_registration();">
</fieldset>
</body>
Edit: Forgot to mention, the drop down menu it’s pulling from is generated dynamically. Here is the code behind that:
function generate_session_list() {
$.ajax({
url: 'http://localhost:8180/Project_2_Highfield/training',
method: 'GET',
dataType: 'json',
success: function (jsondata) {
var len = jsondata.length;
console.log(JSON.stringify(jsondata));
for( var i = 0; i<len; i++){
var session = jsondata[i]['description'];
var id = jsondata[i]['id'];
$("#Session-select").append("<option value='"+ id +"' >"+ session +"</option>");
}
}});
}
I have made the other suggested changes, but am still encountering the error.
Upon further testing, this error appears to occur even when I hard-code the ID to send; i;e,
data: {sessionid: "1", attendeeid: attendeeid},
I am not sure why.
2
Answers
It’s weird when you say that you go both variable logged in console but one of them is sent as null.
Try to add a condition to only execute the ajax code when both variables are not null.
If the Ajax code is not executed, then for sure one of your variables is null or empty, in this case you can check why the select is not getting a value.
(I know you code snippets is out of context), but don’t set the two global variables as a starting point.
Here I use two techniques to for ensuring that the form cannot be submitted before a session and a attendee has been selected. First, the submit button is disabled. I wrapped the AJAX request for session and attendee data in a promise. When the promise is returned the submit button will be enabled.
Second I made the select elements required. This means that the form can only be submitted if the session and the attendee is selected.
Often it is a good idea to avoid using IDs in a form. Use the name attribute instead. You can see that it is easy to refer to the value of one of the select elements in the context of the submit event callback (
put_registration(e)
).