我是Spring MVC的新手。 我有这样的表格,
public @ResponseBody ResultObject doPost(@ModelAttribute("myForm") MyForm myForm){ sysout("myform.input"); }
我可以使用$("#formid").submit(); 我的modelAttribute运行正常,从UI中获取值。
$("#formid").submit();
我的问题是,如何以jquery ajax方式提交此表单? 我试过这个,
$.ajax({ type:"post", url:"/myaction.htm", async: false, dataType: "json", success: function(){ alert("success"); } });
提交表单但是modelAttribute值为空,如何在提交时包含modelAttribute对象(表单正在使用的对象)?
您需要发布数据。 我通常采用的方式是使用以下方法。
var str = $("#myForm").serialize(); $.ajax({ type:"post", data:str, url:"/myaction.htm", async: false, dataType: "json", success: function(){ alert("success"); } });
您没有填充ModelAttributes,因为您没有将任何参数传递给server.Form数据必须发布到服务器
$.post('myaction.htm', $('#formid').serialize())发送ajax post请求。
$.post('myaction.htm', $('#formid').serialize())