繁体   English   中英

Spring Mvc中jQuery Ajax中的JSON响应

[英]JSON response in jQuery Ajax in Spring Mvc

如何在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响应中的每个数据?

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);
    }
    array.put(resObj);
}

try {
    resObjForTable.put("aaData", array);
} catch (JSONException ex) {
    Logger.getLogger(SabbServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return resObjForTable.toString();

提前致谢....

您的控制器类方法应如下所示

    @RequestMapping(value = "fetch")
    @ResponseBody
    public Object methodName() {

        // Your implementation.

        return resObjForTable.toString();
    }

您的C#代码应像这样返回“ Json”

function JsonResult FunctionName (Param){
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);
    }
    array.put(resObj);
}

try {
    resObjForTable.put("aaData", array);
} catch (JSONException ex) {
    Logger.getLogger(SabbServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return json(resObjForTable);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM