jQuery Jacskon 解析器:无法识别的标记“tieheT”:期待“null”、“true”或“false”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13565522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jacskon parser : Unrecognized token 'tieheT': was expecting 'null', 'true' or 'false'
提问by Saket
I am having a problem with passing an array of objects to controller through Hymanson parser, ajax and jquery.
我在通过 Hymanson 解析器、ajax 和 jquery 将对象数组传递给控制器时遇到问题。
My controller looks like this:
我的控制器看起来像这样:
@RequestMapping(value="/saveTimeBooking")
public @ResponseBody ReturnTO saveTimeBooking(@RequestBody TimesheetTO timesheetTO[]) {
// ...
}
My Java Object TimesheetTO looks like this:
我的 Java 对象 TimesheetTO 如下所示:
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:
而且,我使用以下 jquery 代码将时间表对象数组传递给控制器:
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'
提交时,我收到了上面给出的错误:Jacskon parsor exception: Unrecognized token 'tieheT': was expected 'null', 'true' or 'false'
What am I doing wrong ..is there any way to fix this?
我做错了什么..有什么办法可以解决这个问题?
Here is the result of JSON.stringify
:
这是结果JSON.stringify
:
[{"activityMasterId":"1","name":"Financial Implications","mondayEffort":"0","tuesdayEffort":"0","wednesdayEffort":"0","thur??sdayEffort":"0","fridayEffort":"0","saturdayEffort":"0","sundayEffort":"0"},{"act??ivityMasterId":"2","name":"Cost estimation","mondayEffort":"0","tuesdayEffort":"0","wednesdayEffort":"0","thursd??ayEffort":"0","fridayEffort":"0","saturdayEffort":"0","sundayEffort":"0"}]
回答by Manuel Roldan
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.
如有必要,您应该通过调试验证您在运行时传递的值,方法是将其复制到文本编辑器中以对其进行彻底分析。
回答by StaxMan
You need to verify that you are feeding valid JSON: error message indicates this is not the case. It should also give you exact line and row number, which you can use to find the problem with input JSON. From message, I am guessing that either property names are not being quoted, or some String values are not quoted.
您需要验证您提供的是有效的 JSON:错误消息表明情况并非如此。它还应该为您提供确切的行号和行号,您可以使用它们来查找输入 JSON 的问题。从消息中,我猜测要么没有引用属性名称,要么没有引用某些字符串值。
回答by Pradeep Singarakannan
I faced a similar issue and now it is working fine. The issue might be
我遇到了类似的问题,现在它工作正常。问题可能是
data : {
timesheetTO : JSON.stringify(timesheetTO)
}
enclose it in Double quotes
用双引号括起来
data : {
' "timesheetTO" : " '+JSON.stringify(timesheetTO)+' " '
}
回答by ezzadeen
The ajax data should take the string of your JSON object. See the solution here:
ajax 数据应该采用 JSON 对象的字符串。请参阅此处的解决方案:
回答by faheem farhan
Alternatively, you can annotate your class with @RestController
(if it is not already).
Then you dont need to explicitly annotate your request and response with @RequestBody
and @ResponseBody
或者,您可以使用@RestController
(如果还没有)注释您的课程。然后你不需要用@RequestBody
和显式注释你的请求和响应@ResponseBody
So your method signature will change to
所以你的方法签名将更改为
@PostMapping("/saveTimeBooking")
public ReturnTO saveTimeBooking(TimesheetTO timesheetTO[]) {
// ...
}