我试图通过 ajax 调用将我的表单与 Controller 中的 modelattribute 绑定(bind),但我没有获取 bean 中的值,例如 bean 的所有值均为 null
我在alert()中打印了表单数据,它显示了正确的数据,但在 Controller 中它显示了空
这是我的 ajax 调用,console.log() 打印我想要的任何内容,但在 Controller 中我没有获取数据。
Ajax 调用::
function viewBugReport(data) {
var formdata=$("#getAppForm_"+data).serialize();
console.log(formdata);
$.ajax({
url : 'displaybugreport.html',
data :formdata,
processData : false,
contentType : false,
type : 'POST',
success : function(data) {
alert(data);
}
});
}
Controller 代码::
@ResponseBody
@RequestMapping(value = "/displaybugreport.html")
public String viewBugReport(@ModelAttribute BugReportBean1 bugreportbean,
HttpSession session) {
String ResultMessage = "something went wrong!!";
String adminId = "X";
System.out.println(bugreportbean);
try {
AdminBean adminBean = (AdminBean) httpSession.getAttribute(SESSION_KEY_ADMIN);
adminId = adminBean.getUserId().trim();
ResultMessage = bugReportService.submitBugReport(bugreportbean);
} catch (Exception e) {
logger.debug("GADG:" + adminId
+ " :: Exception occured :: viewBugReort POST method :: BugReportController class");
e.printStackTrace();
}
return ResultMessage;
}
最佳答案
由于我看不到完整的 Controller
,并且您没有在所需的端点
中提及任何HTTP
方法,因此您可以尝试像这样:
@RequestMapping(value = "/displaybugreport.html", method = RequestMethod.POST)
关于java - Ajax调用数据不绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54401259/