【问题标题】:How to get JSON response in jQuery Ajax in Spring Mvc? this is my ajax code如何在 Spring Mvc 的 jQuery Ajax 中获取 JSON 响应?这是我的ajax代码
【发布时间】:2015-11-27 10:30:03
【问题描述】:

如何在 Spring Mvc 的 jQuery Ajax 中获得 JSON 响应? 这是我的 ajax 代码:

$.ajax({
               type: 'POST',
               url: "fetch",
               dataType: 'json',
               data:  {clientidedit:clientidedit},
               success: function(data) {

               },
               error: function(jqXHR, textStatus, errorThrown) {
                   alert("error");
               }

          });

这是我的服务。这将返回一个字符串。如何在 ajax 响应中获取每个数据?

    public String getAllClientDetails(String strCode){
       // org.json.JSONObject resObjForTable = new org.json.JSONObject();
        JSONArray array = new JSONArray();
        JSONObject resObj = new JSONObject();
        JSONObject resObjPrd = new JSONObject();
        SabbModelDet detData = sabbDao.getDataForUpdate(strCode);
        List<SabbModelPrd> listPrd = sabbDao.getServicesUpdate(strCode);
            //JSONObject resObj = new JSONObject();
            String clientId = detData.getClientid();
            String clientName = detData.getClientname();
            String customerBaseno = detData.getCustomerbaseno();
            String category = detData.getCategory_1();
            String clientcpt = detData.getClientcpt();
            // add PKB and PRK
            int fileact = detData.getFileact();
            int b2b = detData.getB2b1();
            int collection = detData.getCollection1();
            String primaccno = detData.getPrimaryaccno();
            String accmfromacc = detData.getAccmfromacc();
            String accmtoacc = detData.getAccmtoacc();
              if (StringUtils.isEmpty(clientId)) {
                resObj.put("clientId", org.json.JSONObject.NULL);
            } else {
                resObj.put("clientId", clientId);
            }

            if (StringUtils.isEmpty(clientName)) {
                resObj.put("clientName", org.json.JSONObject.NULL);
            } else {
                resObj.put("clientName", clientName);
            }
            if (StringUtils.isEmpty(customerBaseno)) {
                resObj.put("customerBaseno", org.json.JSONObject.NULL);
            } else {
                resObj.put("customerBaseno",customerBaseno);
            }
            if (StringUtils.isEmpty(category)) {
                resObj.put("category", org.json.JSONObject.NULL);
            } else {
                resObj.put("category",category);
            }
            if (StringUtils.isEmpty(clientcpt)) {
                resObj.put("clientcpt", org.json.JSONObject.NULL);
            } else {
                resObj.put("clientcpt",clientcpt);
            }
             if (fileact>0){
                 resObj.put("fileact", org.json.JSONObject.NULL);
             } else {
                 resObj.put("fileact",fileact);
             }
              if (b2b>0){
                 resObj.put("b2b", org.json.JSONObject.NULL);
             } else {
                 resObj.put("b2b",b2b);
             }
               if (collection>0){
                 resObj.put("collection", org.json.JSONObject.NULL);
             } else {
                 resObj.put("collection",collection);
             }
              if (StringUtils.isEmpty(primaccno)) {
                resObj.put("primaccno", org.json.JSONObject.NULL);
            } else {
                resObj.put("primaccno",primaccno);
            }
               if (StringUtils.isEmpty(primaccno)) {
                resObj.put("accmfromacc", org.json.JSONObject.NULL);
            } else {
                resObj.put("accmfromacc",accmfromacc);
            }
             if (StringUtils.isEmpty(accmtoacc)) {
                resObj.put(" accmtoacc", org.json.JSONObject.NULL);
            } else {
                resObj.put(" accmtoacc", accmtoacc);
            }
            array.put(resObj);

            for(int i=1;i<=listPrd.size();i++){
                for(SabbModelPrd list:listPrd){
                    resObjPrd.put("checkservice"+i, list.getServices1());
                    resObjPrd.put("accountno"+i, list.getAccountno());
                    resObjPrd.put("translimit"+i, list.getTranscationlimit());
                    resObjPrd.put("protocol"+i, list.getProtocol1());
                    resObjPrd.put("clientid"+i, clientId);
                    resObjPrd.put("productid"+i, list.getProductid());
                }
            }
            array.put(resObjPrd);
//            resObjForTable.put("aaData", array);
     return array.toString();

【问题讨论】:

  • 你的控制器在哪里?

标签: java jquery json spring


【解决方案1】:

请在我的 MVC Web API 项目的 App_Start/WebApiConfig.cs 类中添加以下内容

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html") );

【讨论】:

    【解决方案2】:

    您必须有一个 spring 控制器类来访问 ajax 请求并返回一个模型视图作为响应。

    示例控制器

    @RequestMapping(value="/create", method=RequestMethod.GET)
    public ModelAndView createSmartphonePage() {
        ModelAndView mav = new ModelAndView("phones/new-phone");
        mav.addObject("sPhone", new Smartphone());
        return mav;
    }
    

    在 AJAX 和 Spring 上进行谷歌搜索以获取更多示例。

    编辑

    服务不会暴露给控制器级别,因此引入控制器级别会是更好的选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多