jquery序列化form表單使用ajax提交後處理返回的json資料

來源:互聯網
上載者:User

 這篇文章主要介紹了jquery序列化form表單,使用ajax提交後處理返回的json資料的樣本,需要的朋友可以參考下

1、返回json字串:  代碼如下:/** 將一個字串輸出到瀏覽器 */    protected void writeJson(String json) {        PrintWriter pw = null;        try {            servletResponse.setContentType("text/plain;charset=UTF-8");            pw = servletResponse.getWriter();            pw.write(json);            pw.flush();            pw.close();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (pw != null) {                pw.close();            }        }    }  2、通過eval將返回的json字串轉換成json對象: 代碼如下:$.ajax({        data:{            "shipmmsi":shipmmsi,            "shipname":shipname        },        url : "shipbk/findShipMMSIAndName.do",        async : true,        type : "POST",        success : function(data) {            var ships = eval('(' + data + ')');            $("#bindShipmmsiDiv table tbody").html("");            if(ships!=null){                if(ships.length){                    $("#bindShipmmsiDiv").show();                    var trs="";                    for(var i=0;i<ships.length;i++){                        trs+="<tr><td>"+ships[i].mmsi+"</td><td>"+ships[i].vesselName+"</td></tr>";                    }                    $("#bindShipmmsiDiv table tbody").append(trs);                    //給tr註冊點擊事件                    $("#bindShipmmsiDiv table tbody tr").click(function(){                        $(this).addClass('select_tr').siblings().removeClass('select_tr');                    });                    $("#bindShipmmsiDiv table tbody tr").dblclick(function(){                        fillShipMMSIAndName(this);                        $("#bindShipmmsiDiv").hide();                    });                }            }        }    });  3、通過jquery的 $("form").serialize() 可以將form表單的資料序列化後提交到後台,因此通過ajax可以操作form表單並處理返回的資料。 代碼如下:$.ajax({  url : 'deliveryWarrant/update.do',  data : $('#myform').serialize(),  type : "POST",  success : function(data) {    var res = eval('(' + data + ')');    if (res && res.success == true) {          alert(res.message);    location.href="/godownWarrant/findToDeliveryWarrant.do?godownWarrant.code="+$("#myform input[name=godownWarrant.code]").val();    } else {      alert(res.message);    }  }});  4、防止亂碼的處理方法: jsp頁面:charset:utf-8servlet:utf-8filter:utf-8在PrintWriter out = response.getWriter()之前加一句response.setCharacterEncoding("UTF-8")就可以解決亂碼的問題。但是得記住一定要放在聲明PrintWwrite之前。 總之,前台介面,java檔案,資料庫和資料庫的串連都有採用統一編碼,才不會出現亂碼等情況 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.