码迷,mamicode.com
首页 > Web开发 > 详细

jquery异步请求返回JSON

时间:2015-06-08 23:30:47      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:json   jquery   

第一步:前台的Js Demo:

function submitForm(){
var hostname  = $("#hostname").val();
var  port= $("#port").val();
var  validName= $("#validName").val();
var  vp= $("#vp").val();
$.getJSON("req?hostname="+hostname+"&port="+port+"&validName="+validName+"&vp="+vp, function(json){  
var iss = json.issuccess   //前端解析
if(iss=="S") {
$("#etable").append("<tr><td>"+hostname+"</td><td>"+port+"</td><td>"+validName+"</td><td>"+vp+"</td><td>"+json.validcode+"</td></tr>");
}
});
}

第二步:后端Demo:

@RequestMapping("/main/req")
public String save(){
Date date = new Date();
String hostname   = this.request.getParameter("hostname");
String port = this.request.getParameter("port");
String validName = this.request.getParameter("validName");
String   vp = this.request.getParameter("vp");
String validcode ="";
validcode = DateFormatUtil.StringFromDate(date);
validcode = Base64.encode(validcode.getBytes());
String savetime = DateFormatUtil.StringFromStanderDate(date);
String id = GUID.getGUID();
Mailconfig mailconfig = new Mailconfig();
mailconfig.setId(id);
mailconfig.setHostname(hostname);
mailconfig.setPort(port);
mailconfig.setSavetime(savetime);
mailconfig.setValidCode(validcode);
mailconfig.setValidName(validName);
mailconfig.setVp(vp);
boolean res = mailService.save(mailconfig);
Map<String,Object> returnMsg = new HashMap<String,Object>();
try {
if(res == true){
returnMsg.put("issuccess", "S");
returnMsg.put("validcode", validcode);
this.response.getWriter().print(JSONObject.fromObject(returnMsg));
}else {
returnMsg.put("issuccess", "E");
this.response.getWriter().print(JSONObject.fromObject(returnMsg));

}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

jquery异步请求返回JSON

标签:json   jquery   

原文地址:http://blog.csdn.net/java_zhong1990/article/details/46417355

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!