I am new to Spring MVC. I have a form like this,
<form:form action="/myaction.htm" method="post" modelAttribute="myForm" id="formid">
and a controller that returns json
public @ResponseBody ResultObject doPost(@ModelAttribute("myForm") MyForm myForm){ System.out.println("myform.input"); }
I am able to submit this using $("#formid").submit();
and my modelAttribute is working fine, taking values from UI.
my question is, how to submit this form in jquery ajax way? I tried this,
$.ajax({
type:"post",
url:"/myaction.htm",
async: false,
dataType: "json",
success: function(){
alert("success");
}
});
the form is submitted but modelAttribute values are nulls, how to include modelAttribute object(object that form is using) while submitting?
You need to post the data. The way I typically do it is using the following.
var str = $("#myForm").serialize();
$.ajax({
type:"post",
data:str,
url:"/myaction.htm",
async: false,
dataType: "json",
success: function(){
alert("success");
}
});
your ModelAttributes are not populated as you are not passing any params to server.Form data has to be posted to server
$.post('myaction.htm', $('#formid').serialize())
to send ajax post request.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.