天天看點

html頁面發送請求處理json

通過JS方式發送請求

htmlcode:

通過onclick觸發

<a href="" onclick="del()" >删除</a>

<script type="text/javascript">
    function del(){
        if(window.confirm("是否删除?")){
            $.ajaxSetup({ 
                async : false 
            });
            var id = document.getElementById("pid").value;
            var url = "${contextPath}/publishEdit/delMsg";
            var paras = {id : id};
            $.get(url,paras,function(data){
                if(data.RETCODE ==="1"){
                    alert("删除成功!")
                    window.location = "${contextPath}/published";
                }else{
                      alert("删除失敗!");
                      return;
                  }
            });
        }else{
            window.location = "${contextPath}/published";
        }
    }
    </script>
           

controller:

public void delMsg(){
        String id = getPara("id");
        boolean isDel = publishService.delContent(id);
        Record r = new Record();
        if(isDel){   
            r.set("retcode", "1");
        }else {
            r.set("retcode", "0");
        }
        renderJson(r.toJson());
    }