Java 如何在SpringMVC的jQueryAjax中获得JSON响应?这是我的ajax代码
如何在SpringMVC的jQueryAjax中获得JSON响应? 这是我的ajax代码:Java 如何在SpringMVC的jQueryAjax中获得JSON响应?这是我的ajax代码,java,jquery,json,spring,Java,Jquery,Json,Spring,如何在SpringMVC的jQueryAjax中获得JSON响应? 这是我的ajax代码: $.ajax({ type: 'POST', url: "fetch", dataType: 'json', data: {clientidedit:clientidedit}, success: function(data) {
$.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();
public字符串getAllClientDetails(字符串strCode){
//org.json.JSONObject ResObjectable=new org.json.JSONObject();
JSONArray数组=新的JSONArray();
JSONObject ResObject=新的JSONObject();
JSONObject ResObjectPRD=新的JSONObject();
SabbModelDet detData=sabbDao.getDataForUpdate(strCode);
List listPrd=sabbDao.getServicesUpdate(strCode);
//JSONObject ResObject=新的JSONObject();
字符串clientId=detData.getClientid();
字符串clientName=detData.getClientname();
字符串customerBaseno=detData.getCustomerbaseno();
字符串category=detData.getCategory_1();
字符串clientcpt=detData.getClientcpt();
//添加PKB和PRK
int fileact=detData.getFileact();
int b2b=detData.getB2b1();
int collection=detData.getCollection1();
字符串primaccno=detData.getPrimaryaccno();
字符串accmfromac=detData.getaccmfromac();
字符串accmtoacc=detData.getAccmtoacc();
if(StringUtils.isEmpty(clientId)){
put(“clientId”,org.json.JSONObject.NULL);
}否则{
resObj.put(“clientId”,clientId);
}
if(StringUtils.isEmpty(clientName)){
put(“clientName”,org.json.JSONObject.NULL);
}否则{
resObj.put(“客户名称”,客户名称);
}
if(StringUtils.isEmpty(customerBaseno)){
put(“customerBaseno”,org.json.JSONObject.NULL);
}否则{
决议付诸表决(“customerBaseno”,customerBaseno);
}
if(StringUtils.isEmpty(类别)){
put(“category”,org.json.JSONObject.NULL);
}否则{
决议草案(“类别”,类别);
}
if(StringUtils.isEmpty(clientcpt)){
put(“clientcpt”,org.json.JSONObject.NULL);
}否则{
决议书(“客户CPT”,客户CPT);
}
如果(fileact>0){
put(“fileact”,org.json.JSONObject.NULL);
}否则{
决议草案(“文件法案”,文件法案);
}
如果(b2b>0){
resObj.put(“b2b”,org.json.JSONObject.NULL);
}否则{
处置权(“b2b”,b2b);
}
如果(集合>0){
put(“collection”,org.json.JSONObject.NULL);
}否则{
处置权(“托收”,托收);
}
if(StringUtils.isEmpty(primacno)){
resObj.put(“primaccno”,org.json.JSONObject.NULL);
}否则{
决议草案(“Primacno”,Primacno);
}
if(StringUtils.isEmpty(primacno)){
put(“accmfromac”,org.json.JSONObject.NULL);
}否则{
决议草案(“accmfromacc”,accmfromacc);
}
if(StringUtils.isEmpty(accmtoacc)){
put(“accmtoacc”,org.json.JSONObject.NULL);
}否则{
决议书(“accmtoacc”,accmtoacc);
}
array.put(resObj);
对于(int i=1;i请在我的MVC Web API项目中添加以下App_Start/WebApiConfig.cs类
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html") );
您必须有一个springcontroller类来访问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上进行google搜索,以获得更多示例
编辑
服务不暴露于控制器级别,因此引入控制器级别是更好的选择。控制器在哪里?