Ajax請求資料與刪除資料後重新整理頁面

來源:互聯網
上載者:User

標籤:bool   mod   encoding   get   set   lock   trace   row   tco   

1.ajax非同步請求資料後填入模態框

請求資料的按鈕(HTML)

<a class="queryA" href="javascript:void(0)"   onclick="query(${register.id})"> <span class="glyphicon glyphicon-search"></span></a>

 ajax非同步請求資料後給id為queryInfo的模態框賦值並彈出模態框(JS)

//查詢單個function query(id) {    $.ajax({        url : "/small/productServlet",        async : true,        type : "POST",        data : {            "type" : "query",            "id" : id        },        // 成功後開啟模態框        success : showQuery,        error : function() {            alert("請求失敗");        },        dataType : "json"    });}// 查詢成功後向模態框插入資料並開啟模態框。data是返回的JSON對象function showQuery(data) {    $("#name1").val(data.name);    $("#xinghao1").val(data.xinghao);    $("#address1").val(data.address);    $("#department1").val(data.department);    $("#unit1").val(data.unit);    $("#number1").val(data.number);    $("#price1").val(data.price);    $("#totalprice1").val(data.totalprice);    $("#come1").val(data.come);    $("#buytime1").val(data.buytime);    $("#useperson1").val(data.useperson);    $("#handleperson1").val(data.handleperson);    $("#admini1").val(data.admini);    // 顯示模態框    $(‘#queryInfo‘).modal(‘show‘);}

 

幕後處理ajax請求並返回JSON串(Java)

protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        RegisterService service = new RegisterServiceImpl();        request.setCharacterEncoding("UTF-8");        response.setCharacterEncoding("UTF-8");        response.setContentType("text/html; charset=UTF-8");        String type = request.getParameter("type");        if (type != null && "add".equals(type)) {            try {                this.add(request, response, service);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }                if (type != null && "query".equals(type)) {            try {                this.query(request, response, service);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }    protected void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }    public void query(HttpServletRequest request, HttpServletResponse response, RegisterService service)            throws Exception {        Integer id = Integer.parseInt(request.getParameter("id"));        Register register = service.getRegisterById(id);        Gson gson = new Gson();        String json = gson.toJson(register);        System.out.println(json);        response.getWriter().write(json);    }

 

 JSON串:

{"id":16,"name":"測試名稱16","address":"測試地址2","department":"測試部門2","unit":"測試單位2","number":1,"price":1.5,"totalprice":1.5,"come":"來源2","buytime":"八月 23, 2017","useperson":"使用人2","handleperson":"經辦人2","admini":"管理員部門2"}

 

 

 

2.ajax非同步請求刪除資料後重新整理頁面

 請求刪除的按鈕(HTML)

<a class="" href="javascript:void(0)" onclick="deleteInfo(${register.id})"> <span  class="glyphicon glyphicon-trash"></a>

 

 Ajax請求刪除資料後重新整理頁面(JS)

// ajax非同步刪除後重新整理頁面function deleteInfo(id) {    alert("dele");    $.ajax({        url : "/small/productServlet",        async : true,        type : "POST",        data : {            "type" : "delete",            "id" : id        },        success : function(data) {            alert(data);            // 刪除成功後重新整理頁面            window.location.reload();        },        error : function() {            alert("請求失敗");        },        dataType : "text"    });}

 

 幕後處理刪除的ajax請求(Java)

    // 根據id刪除    public void delete(HttpServletRequest request, HttpServletResponse response, RegisterService service)            throws Exception {        Integer id = Integer.valueOf(request.getParameter("id"));        boolean result = service.deleteRegisterById(id);        if (result) {            response.getWriter().write("success delete!");        }    }

 

Ajax請求資料與刪除資料後重新整理頁面

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.