SSMバックエンドからフロントエンドjsへのデータ転送


一:バックグラウンドからフロントへの一般データの転送
バックグラウンド転送方法:
        int XXXX = 0;
        String YYYY = "";
        try {
            //           
            PrintWriter out = response.getWriter();
            Map hashmap = new HashMap<>();
            hashmap.put("xxxx", XXXX);
            hashmap.put("yyyy", yyyy);
            out.println(JSON.toJSON(hashmap));
            out.flush();
            out.close();
        } catch (
                IOException e) {
            e.printStackTrace();
        }

フロントJs受信方法:
 $.ajax({
        url: "",
         type: "post",
        dataType: "json",
        data: {},
        success: function (data) {
            //      

            var json = JSON.stringify(data);
            var a = eval('(' + json + ')');// json   ,     eval(json)
            
            alert(a.xxxx);//     
            alert(a.yyyy);//    
        },

        error: function (er) { //  ,    
        }
    });

二:バックグラウンドからフロントにListタイプデータとその他の一般データを渡す
バックグラウンド転送方法:
        List list =.... ;
 
      try {
            //           
            PrintWriter out = response.getWriter();
            Map hashmap = new HashMap<>();
            hashmap.put("productList", productList);
            hashmap.put("xxxx",XXXX);
            out.println(JSON.toJSON(hashmap));
            System.out.println(JSON.toJSON(hashmap));
            out.flush();
            out.close();
        } catch (
                IOException e) {
            e.printStackTrace();
        }

フロントjs受信:
    $.ajax({
            url: "/generalManager/queryProductInfo", //  url
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(product),
            success: function (data) {
                //alert("     " + data.length + "   ")
                $("#btn_clear").click();
                //  ,    
                var json = JSON.stringify(data);//   Json  
  
                var  a = eval('('+json+')');

                //   list    
                alert(a.xxxx);

                var list = eval(a.productList);// json   

                $('#product_tbody tr').empty();//    
                $.each(list, function (index, item) {
                    
                      alert(item.xxxx)
                });
                $('#product_tbody').append(str);

                PageInfo();//      
                alert(totalCount + '  COUNT')
                alert(endPage + " endPage")
            },

            error: function (er) {          //  ,    
                alert('    ');
                //  alert(er)

            }
        });
        // productDetail();
    });

三:バックグラウンドからフロントにListタイプデータを渡す
バックグラウンド転送方法:
        List list = 。。。
        String xxxList = JSON.toJSONString(list);

        //           
        try {
            PrintWriter out = response.getWriter();
            out.println(xxxList);//     list   
            out.flush();
            out.close();
        } catch (
                IOException e) {
            e.printStackTrace();
        }

フロントjsの受け入れ方法:
        $.ajax({
            url: "", //  url
            type: "",
            dataType: "",
            contentType: "application/json;charset=UTF-8",
            data:{},
            success: function (data) {
                //       list    

                var json = JSON.stringify(data);//   Json  (  list      json)
                var a = eval(json);// json   (     eval('('+json+')'));

                //  list     
                 $.each(a, function (index, item) {//index  list   ,item   list     
                   alert(item.xxx);//  list       
                   alert(item.yyy);//  list       
                });
            },
            error: function (er) {          //  ,    
             }
        });