ajax+springmvc实现C与View之间的数据交流方法
jQuery.post(url,[data],[callback],[type])
url,[data],[callback],[type]String,Map,Function,StringV1.0url:发送请求地址。
data:待发送Key/value参数。
callback:发送成功时回调函数。
type:返回内容格式,xml,html,script,json,text,_default。
套用格式:
$.post("test.php",function(data){ alert("DataLoaded:"+data); }); $.get("comment/getComments?parentId="+parentId+"&topicId="+topicId,function(data){ varappendButton=""; varappend=""; if(data!=""){ vararr=data.split("$"); varallTr=""; for(vari=0;i"; appendButton=appendButton+table+" "+time+" 回复"; } appendButton=appendButton+" 我也说一句"; } appendButton=appendButton+"
后台:
@RequestMapping(value="/saveAndGetComments",params={"topicId","parentId"},method=RequestMethod.POST) @ResponseBody publicStringsaveAndGetComments(longtopicId,Commentcomment,longparentId)throwsUnsupportedEncodingException{ comment.setParentId(parentId); commentService.save(comment,topicId); Listcomments=commentService.listByCommentId(parentId); returnappend(comments); } privateStringappend(List comments){ StringBuffersb=newStringBuffer(); for(inti=0;i 注意,用springmvc3的注解@responseBody来传递参数。
经常用到的js函数:
上面由于使用json来传递的数据,而js解析json传过来的日期时,不是我们想要的格式,这时需要对日期进行操作:
首先传过去的日期将它设为time传过去date.getTime()
然后再在js中操作:
vardate="/Date("+time+")/"; date=DateFormat(date); /** *处理时间 *@paramvalue *@returns{String} */ functionDateFormat(value){ vardate=newDate(parseInt(value.replace("/Date(","").replace(")/",""),10)); varmonth=date.getMonth()+1<10?"0"+(date.getMonth()+1):date.getMonth()+1; varcurrentDate=date.getDate()<10?"0"+date.getDate():date.getDate(); varHours=date.getHours()<10?"0"+date.getHours():date.getHours(); varMinutes=date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes(); varSeconds=date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds(); returndate.getFullYear()+"/"+month+"/"+currentDate+""+Hours+":"+Minutes+":"+Seconds; }以上这篇ajax+springmvc实现C与View之间的数据交流方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。