form表单通过ajax请求后台,表单数据序列化

在form中天机按钮及点击事件,将form元素作为参数传入

js方法:

function mycart(form){
    $.ajax({
        type:"post",
        url:"/cart/cart.action",
        datatype:"text",
        data:$(form).serialize(),
        success:function (info) {
            alert(info)
        },
        error:function () {
            alert("error!")
        }
    })
};

后台一定加上@ResponseBody注解返回数据

@RequestMapping("/cart")
@ResponseBody
public  String Cart(HttpSession session, HttpServletResponse response,Integer id) throws Exception{

   return "ok";
}

你可能感兴趣的:(form表单通过ajax请求后台,表单数据序列化)