繁体   English   中英

如何使用modelAttribute在ajax(jquery)中提交spring表单

[英]How to submit spring form in ajax(jquery) with modelAttribute

我是Spring MVC的新手。 我有这样的表格,

<form:form action="/myaction.htm" method="post" modelAttribute="myForm" id="formid">以及一个返回json的控制器

public @ResponseBody ResultObject doPost(@ModelAttribute("myForm") MyForm myForm){ System.out.println("myform.input"); }

我可以使用$("#formid").submit(); 我的modelAttribute运行正常,从UI中获取值。

我的问题是,如何以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请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM