首页 > 语言 > JavaScript > 正文

jquery序列化form表单使用ajax提交后处理返回的json数据

2024-05-06 14:28:53
字体:
来源:转载
供稿:网友

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>";

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选