ajax提交form表单,后台接收参数

controller:

//批量删除delBatch
    @RequestMapping("delBatch.do")
    public void delBatch(Integer[] ids,HttpServletResponse response) throws IOException{
        String str = productService.delBatch(ids);
        response.getWriter().write(str);

    }

jsp:

<form id="delBatch" name="delBatch" action="##" method="post" >
<td>
    <input type="checkbox" name="ids" id="select" value="${item.pid }" />编号
    <a href="${pageContext.request.contextPath }/shop/item/updateItem.jsp">编辑a>
    <a href="javascript:void(0);" onclick="del(${item.pid})" >删除a>
td>
 选择:<input type="button" value="批量删除" onclick="delbatch()"/>
 form>

ajax:

function delbatch(){

        if($('#delBatch').serialize() == ""){
            return false;
        }else {
             $.ajax({

                        type: "post",
                        dataType: "text",
                        url: "${pageContext.request.contextPath}/product/delBatch.do" ,
                        data: $('#delBatch').serialize(),
                        success: function (data) {

                            if(data == "ok"){
                                alert("删除成功")

                            }else{
                                alert("该商品已存在订单,删除失败")

                            }

                        },
                        error : function() {
                            alert("异常!");
                        }
                    });
        }

    }

注意:ajax的form,提交不能是submit,action不能有url,
$(‘#delBatch’).serialize() 方法通过序列化表单值,创建 URL 编码文本字符串,简单的说就是传给后台的值

你可能感兴趣的:(ajax提交form表单,后台接收参数)