sevlet中的代码如下,其中base_cn等等都是通过数据库Dao方法查询出来的整型变量,然后转换为json格式字符串,传回前台和页面上的数字比较。
int base_rn = new RadioDao().getRadioNumber(base);
int base_cn = new CheckBoxDao().getCheckBoxNumber(base);
int base_jn = new JudgeDao().getJudgeNumber(base);
int base_bn = new BlankDao().getBlankNumber(base);
response.setContentType("application/json; charset=utf-8");
String jsonStr = "{\"base_rn\":base_rn,\"base_cn\":base_cn,\"base_jn\":base_jn,\"base_bn\":base_bn}";
PrintWriter out = null;
try {
out = response.getWriter();
out.write(jsonStr);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
前台的ajax如下:
$.ajax({
url : "servlet/GetPaperServlet",
type : "POST",
data : {
"account" : account.val(),
"true_name" : true_name.val(),
"title" : title.val(),
"base" : base.val(),
"difficult" : difficult.val(),
"time" : time.val(),
"radio_number" : radio_number.val(),
"checkbox_number" : checkbox_number.val(),
"judge_number" : judge_number.val(),
"blank_number" : blank_number.val(),
"radio_score" : radio_score.val(),
"checkbox_score" : checkbox_score.val(),
"judge_score" : judge_score.val(),
"blank_score" : blank_score.val(),
"sum_score" : sum_score.val()
},
dataType : "json",
success : function(jsonStr) {
alert("comming");
alert(jsonStr);
if(jsonStr.base_rn<radio_number){
alert("题库中单选题数量不足,请减少题量!");
return false;
}else{
alert("dd");
}
},
error : function(er) {
alert(er.message);
}
});
执行后,页面直接弹出undefined的警示框,请问大神正确的做法是什么?才能正确的实现相应的功能!