バックエンド戻り値はjson形式で返され、フロントエンドはjson形式で受信されます.

11448 ワード

任意のクラスを例に挙げると、この例は企業の主なカテゴリを検索する前の5つの事項です.
一、json配列のフォーマットでフロントエンドに戻る
(1)バックエンドは結果をparamにバインドし,json配列のフォーマットとしてフロントエンドに返す.
/**
     *          5  
     * @param request
     * @param response
     * @param config
     * @throws Exception
     * @author hongxy
     * 2017 6 1   2:21:14
     */
    public void getEnterpriseMainCategory(HttpServletRequest request,
            HttpServletResponse response, ServletConfig config) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");  
        Map param = new HashMap();
        PrintWriter wirte = null; 
        //      
        String custName = RequestUtil.getString(request, "companyName");
        //          
        if (StringUtils.isBlank(custName)) {
            param.put("status", "400");
            param.put("desc", "      !");
        } else {
            workService = new WorkServiceImpl();
            //         
            Map enterpriseInfo = workService.getEnterpriseInfoByCustName(custName);
            //      
            if (enterpriseInfo == null) {
                param.put("status", "400");
                param.put("desc", "       !");
            } else {//     ,         5  
                //                    
                String approveTypeList = workService.getEnterpriseWorksInfoByCustName(custName);
                //            
                String custNameListByIndustry  = workService.getEnterpriseNameByIndustry((String) enterpriseInfo.get("INDUSTRY"));
                //         5  
                List mainProjectList = workService.getApproveInfoList(custNameListByIndustry,approveTypeList);
                param.put("status", "200");
                param.put("desc", "    ");
                param.put("data", mainProjectList);
            }
        }
      //  JSONArray     JSON   
          JSONArray array = JSONArray.fromObject(param); 
          wirte = response.getWriter(); 
          wirte.print(array); 
    }

(2)フロントエンドは先に受け取ったデータをjson形式に変換して、さもなくば中の値を取得することができなくて、Ajaxが返す値がデフォルトで文字列のタイプなためです
受信した値をjson形式のコアコードに変換します:(具体的な値はフロントエンドコンソールに出力でき、値を取りやすい)
var msg=jQuery.parseJSON(msg);
$.ajax({
       url: '${path.appMain}?service=work&func=getEnterpriseMainCategory',
       async: false,
       type: 'POST',
       data: {
           companyName:companyName
       },
       success: function(msg){
               var msg=jQuery.parseJSON(msg);
               var mainProjectList = "";
              mainProjectList +="
  • あなたの「主なプロジェクトカテゴリ」と同じ企業で、最も多くの事項を処理するのは
  • です.
      for (var i = 0; i < msg[0].data.length; i++) {
    mainProjectList += "
  • "+ (i+1) + "." + msg[0].data[i].approveName + "
  • ";
    }
    $('#mainProjectList').html(mainProjectList);
    }
    });
    二、jsonの形式でフロントエンドに戻る(よく使う)
    (1)バックエンドは結果をdataにバインドし,jsonのフォーマットとしてフロントエンドに返す.
         /**
         *          5  
         * @param request
         * @param response
         * @param config
         * @throws Exception
         * @author hongxy
         * 2017 6 1   2:21:14
         */
        public void getEnterpriseMainCategory(HttpServletRequest request,
                HttpServletResponse response, ServletConfig config) throws Exception {
            request.setCharacterEncoding("UTF-8");
            JSONObject json = new JSONObject();
            Map param = new HashMap();
            //      
            String custName = RequestUtil.getString(request, "companyName");
              //          
              if (StringUtils.isBlank(custName)) {
                  json.put("status", "400");
                  json.put("desc", "      !");
              } else {
                  workService = new WorkServiceImpl();
                  //         
                  Map enterpriseInfo = workService.getEnterpriseInfoByCustName(custName);
                //      
                if (enterpriseInfo == null) {
                      json.put("status", "400");
                      json.put("desc", "       !");
                } else {//     ,         5  
                    //                    
                    String approveTypeList = workService.getEnterpriseWorksInfoByCustName(custName);
                    //            
                    String custNameListByIndustry  = workService.getEnterpriseNameByIndustry((String) enterpriseInfo.get("INDUSTRY"));
                    //         5  
                    List mainProjectList = workService.getApproveInfoList(custNameListByIndustry,approveTypeList);
                      json.put("status", "200");
                      json.put("desc", "    ");
                      json.put("data", mainProjectList);
                }
              }
              //     
             SysInfo.responseJsonMsg(response, json.toString());
        }

    (2)フロントエンドでjsonデータを受信してフロントエンドで表示する
    $.ajax({
           url: '${path.appMain}?service=work&func=getEnterpriseMainCategory',
           async: false,
           type: 'POST',
           data: {
               companyName:companyName
           },
           success: function(msg){
                   var mainProjectList = "";
                mainProjectList +="
  • あなたの「主なプロジェクトカテゴリ」と同じ企業で、最も多くの事項を処理するのは
  • です.
    if(msg.data.length == 0){
    mainProjectList+="
  • 関連事項が見つからない!
  • ";
    } else{
    for (var i = 0; i < msg.data.length; i++) {
    mainProjectList += "
  • "+ (i+1) + "." + msg.data[i].approveName + "
  • ";
    }
    }
    $('#mainProjectList').html(mainProjectList);
    }
    });
    転載先:https://www.cnblogs.com/xuegu/p/6951668.html