Attempting to send data from a jsp page containing datetime
, zoneid
, checked checkboxes with values, and unchecked checkboxes with null values.
Despite sending all this as json to my spring rest controller, I noticed in debug mode that the controller only received two pieces of data (one checkbox that I clicked and the submitted input value). It's puzzling where the rest of the data disappeared to.
While sending data through postman works flawlessly, I'm encountering difficulties when trying to send it from the jsp page.
Prior to this, I used @RequestBody
in the controller to retrieve data of type pojo class object, but it resulted in a content-type unsupported error. I then switched to using @RequestParam
for map type data, which resolved the error. However, the reason behind the initial error remains unclear.
jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<!-- Static content -->
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="/resources/css/style.css">
<script type="text/javascript" src="/resources/js/app.js"></script>
</head>
<body>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id = "schedulejob" action="scheduleJob" method="POST">
<label>
<input type="checkbox" id = "corporateClientCare" name="corporateClientCare" value="corporateClientCare" class="input_checkbox"> CorporateClientCare
</label>
<label>
<input type="checkbox" id = "dayforce" name="dayforce" value="dayforce" class="input_checkbox"> Dayforce </label>
<label>
<input type="checkbox" id = "tss" name="tss" value="tss" class="input_checkbox"> TSS </label>
<label>
<input type="checkbox" id = "multimax" name="multimax" value="multimax" class="input_checkbox"> Multimax</label>
<label> <input type="checkbox" id = "arcot" name="arcot" value="arcot" class="input_checkbox"> Arcot <br/></label>
<input type="datetime-local" id="dateTime" name = "dateTime" value="2019-06-04T15:25:33">
<input type="submit" name="Scheduler" value="Scheduler" class="submit"/></form>
<script>
jQuery(document).ready(function($) {
$("#schedulejob").submit(function(){
var scheduleRequest = {};
scheduleRequest["corporateClientCare"] = verifychecked("corporateClientCare");
scheduleRequest["dayforce"] = verifychecked("dayforce");
scheduleRequest["tss"] = verifychecked("tss");
scheduleRequest["multimax"] = verifychecked("multimax");
scheduleRequest["arcot"] = verifychecked("arcot");
scheduleRequest["dateTime"] = document.getElementById("dateTime").value;
scheduleRequest["timeZone"] = "Asia/Kolkata";
$.ajax({
type : form.attr('method'),
contentType : "application/json;charset=utf-8",
url : form.attr('action'),
data : JSON.stringify(scheduleRequest),
dataType : 'json',
success : function(data) {
}
});
});
function verifychecked(value) {
var varr = '';
if(document.getElementById(value).checked)
{
varr = value;
}
else{
varr = null;
}
return varr;
}
});
</script>
</body>
</html>
controller
@Autowired
private Scheduler scheduler;
@PostMapping("/scheduleJob")
public ResponseEntity<ScheduleResponse> scheduleJobs(@RequestBody ScheduleRequest scheduleRequest) {
try {
System.out.println("___________IN CONTROLLER__________");
System.out.println("--------------zone-----------");
ZonedDateTime dateTime = ZonedDateTime.of(scheduleRequest.getDateTime(), scheduleRequest.getTimeZone());
System.out.println("--------------date is-----------" + dateTime);
System.out.println("dateTime is " + dateTime);
if(dateTime.isBefore(ZonedDateTime.now())) {
ScheduleResponse scheduleResponse = new ScheduleResponse(false,
"dateTime must be after current time");
System.out.println("--------------1-------------------");
return ResponseEntity.badRequest().body(scheduleResponse);
}
//rest code
Despite thorough checks in debug mode, the map only contained 2 keys, leaving me puzzled about the missing data.