I am having a problem with passing an array of objects to controller through jackson parser, ajax and jquery.
My controller looks like this:
@RequestMapping(value="/saveTimeBooking")
public @ResponseBody ReturnTO saveTimeBooking(@RequestBody TimesheetTO timesheetTO[]) {
// ...
}
My Java Object TimesheetTO looks like this:
public class TimesheetTO implements Serializable {
private static final long serialVersionUID = 1L;
private int activityMasterId;
private String name;
private BigDecimal mondayEffort=new BigDecimal(0);
private BigDecimal tuesdayEffort=new BigDecimal(0);
private BigDecimal wednesdayEffort=new BigDecimal(0);
private BigDecimal thursdayEffort=new BigDecimal(0);
private BigDecimal fridayEffort=new BigDecimal(0);
private BigDecimal saturdayEffort=new BigDecimal(0);
private BigDecimal sundayEffort=new BigDecimal(0);
public int getActivityMasterId() {
return activityMasterId;
}
public String getName() {
return name;
}
public BigDecimal getMondayEffort() {
return mondayEffort;
}
public BigDecimal getTuesdayEffort() {
return tuesdayEffort;
}
public BigDecimal getWednesdayEffort() {
return wednesdayEffort;
}
public BigDecimal getThursdayEffort() {
return thursdayEffort;
}
public BigDecimal getFridayEffort() {
return fridayEffort;
}
public BigDecimal getSaturdayEffort() {
return saturdayEffort;
}
public BigDecimal getSundayEffort() {
return sundayEffort;
}
public void setActivityMasterId(int activityMasterId) {
this.activityMasterId = activityMasterId;
}
public void setName(String name) {
this.name = name;
}
public void setMondayEffort(BigDecimal mondayEffort) {
this.mondayEffort = mondayEffort;
}
public void setTuesdayEffort(BigDecimal tuesdayEffort) {
this.tuesdayEffort = tuesdayEffort;
}
public void setWednesdayEffort(BigDecimal wednesdayEffort) {
this.wednesdayEffort = wednesdayEffort;
}
public void setThursdayEffort(BigDecimal thursdayEffort) {
this.thursdayEffort = thursdayEffort;
}
public void setFridayEffort(BigDecimal fridayEffort) {
this.fridayEffort = fridayEffort;
}
public void setSaturdayEffort(BigDecimal saturdayEffort) {
this.saturdayEffort = saturdayEffort;
}
public void setSundayEffort(BigDecimal sundayEffort) {
this.sundayEffort = sundayEffort;
}
public TimesheetTO(){}
public TimesheetTO( String activityMasterId,
String name,
String mondayEffort,
String tuesdayEffort,
String wednesdayEffort,
String thursdayEffort,
String fridayEffort,
String saturdayEffort,
String sundayEffort)
{
this.activityMasterId=Integer.parseInt(activityMasterId);
this.name=name;
this.mondayEffort=BigDecimal.valueOf(Double.parseDouble(mondayEffort));
this.tuesdayEffort=BigDecimal.valueOf(Double.parseDouble(tuesdayEffort));
this.wednesdayEffort=BigDecimal.valueOf(Double.parseDouble(wednesdayEffort));
this.thursdayEffort=BigDecimal.valueOf(Double.parseDouble(thursdayEffort));
this.fridayEffort=BigDecimal.valueOf(Double.parseDouble(fridayEffort));
this.saturdayEffort=BigDecimal.valueOf(Double.parseDouble(saturdayEffort));
this.sundayEffort=BigDecimal.valueOf(Double.parseDouble(sundayEffort));
}
}
And, I am using the following jquery code to pass the array of timesheet objects to controller:
var datacounter=0;
/*var totalcount=0;*/
var timesheetTO = new Array();
$("#timeSheetTableId input[id^=activityMasterId]").each(function() {
$('#activityMasterId_'+datacounter).removeAttr('disabled');
$('#name_'+datacounter).removeAttr('disabled');
timesheetTO.push({
"activityMasterId": $('#activityMasterId_'+datacounter).val(),
"name": $('#name_'+datacounter).val(),
"mondayEffort": new Integer($('#mondayEffort_'+datacounter).val()),
"tuesdayEffort": $('#tuesdayEffort_'+datacounter).val(),
"wednesdayEffort": $('#wednesdayEffort_'+datacounter).val(),
"thursdayEffort": $('#thursdayEffort_'+datacounter).val(),
"fridayEffort": $('#fridayEffort_'+datacounter).val(),
"saturdayEffort": $('#saturdayEffort_'+datacounter).val(),
"sundayEffort": $('#sundayEffort_'+datacounter).val()
});
datacounter=datacounter+1;
});
var url = contextRoot + "timesheet/saveTimeBooking.htm";
$.ajax({
type : 'POST',
url : url,
async : false,
timeout : 5000,
contentType: 'application/json',
dataType: 'json',
data : {
timesheetTO : JSON.stringify(timesheetTO)
},
success : function(data, textStatus) {
alert('successful');
},
error : function(xhr, textStatus, errorThrown) {
alert('request failed in saving timesheet:' + errorThrown + " " + textStatus + " "
+ xhr.toString());
}
});
While submitting, I am getting the error as given above : Jacskon parsor exception: Unrecognized token 'tieheT': was expecting 'null', 'true' or 'false'
What am I doing wrong ..is there any way to fix this?
Here is the result of JSON.stringify
:
[{"activityMasterId":"1","name":"Financial Implications","mondayEffort":"0","tuesdayEffort":"0","wednesdayEffort":"0","thursdayEffort":"0","fridayEffort":"0","saturdayEffort":"0","sundayEffort":"0"},{"activityMasterId":"2","name":"Cost estimation","mondayEffort":"0","tuesdayEffort":"0","wednesdayEffort":"0","thursdayEffort":"0","fridayEffort":"0","saturdayEffort":"0","sundayEffort":"0"}]
The problem is that you are passing a string value which is lacking quotation marks, and the compiler is therefore expecting a boolean/null object.
You should verify what value you are passing at runtime through debugging, by copying it in a text editor to analyze it thoroughly, if necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With