【发布时间】: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();
【问题讨论】:
-
你的控制器在哪里?