Home >Web Front-end >JS Tutorial >jquery serializes the form form and processes the returned json data after submission using ajax_jquery

jquery serializes the form form and processes the returned json data after submission using ajax_jquery

WBOY
WBOYOriginal
2016-05-16 16:57:252279browse

1. Return json string:

Copy code The code is as follows:

/**Output a string to the browser*/
protected void writeJson(String json) {
PrintWriter pw = null;
try {
servletResponse.setContentType("text/plain;charset=UTF-8");
pw = servletResponse.getW riter() ;
pw.write(json);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace( );
                                                                                                                >

2. Convert the returned json string into a json object through eval:



Copy the code


The code is as follows:


$.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                        trs ="" ships[i].mmsi "" ships[i].vesselName "";
                    }
                    $("#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. Methods to prevent garbled characters:

jsp page: charset: utf-8
servlet:utf-8
filter:utf-8
Add
response.setCharacterEncoding(" before PrintWriter out = response.getWriter() UTF-8") can solve the problem of garbled characters.
But remember to put it before declaring PrintWwrite.

In short, the front-end interface, java files, databases and database connections all use unified encoding, so that there will be no garbled characters

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn