今天来说下使用ajax 提交form表单,希望可以帮助到大家,进行留存一下。
html页面
<form id="apply_link_form">
<input type="text" name="link_name" id="link_name" placeholder="如:言曌博客" required="required">
<input type="text" name="link_url" id="link_url" placeholder="如:https://liuyanzhao.com" required="required">
<input type="text" name="link_description" id="link_description" placeholder="如:一个后端开发者的成长笔记" >
<input type="text" name="link_owner_contact" id="link_owner_contact" placeholder="如:邮箱service@liuyanzhao.com" required="required">
<input id="submit" name="submit" type="submit" value="提交申请" >
<
</form>
js部分
//ajax提交信息
$("#apply_link_form").submit(function(){
parent.layer.close(index); //再执行关闭
$.ajax({
async: false,
type: "POST",
url:'${pageContext.request.contextPath}/link/apply',
contentType : "application/x-www-form-urlencoded; charset=utf-8",
data:$("#apply_link_form").serialize(),
dataType: "text",
success: function () {
},
error: function () {
}
})
})
后台部分
@RequestMapping(value = "link/apply",method = {RequestMethod.POST})
@ResponseBody
public void applyLink(HttpServletRequest request) throws Exception {
String linkName = request.getParameter("link_name");;
String linkUrl = request.getParameter("link_url");
String linkDescription = request.getParameter("link_description");
String linkOwnerContact = request.getParameter("link_owner_contact");
LinkCustom linkCustom = new LinkCustom();
linkCustom.setLinkName(linkName);
linkCustom.setLinkUrl(linkUrl);
linkCustom.setLinkDescription(linkDescription);
linkCustom.setLinkOwnerContact(linkOwnerContact);
linkCustom.setLinkStatus((byte) 0);
linkCustom.setLinkCreateTime(new Date());
linkCustom.setLinkUpdateTime(new Date());
linkService.applyLink(linkCustom);
}