介绍
这篇文章主要介绍ajax + springmvc如何实现C与观点之间的数据交流方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
jQuery。post (url,(数据),(回调),[型])
url,(数据),(回调),[型]字符串,地图,函数,StringV1.0url:发送请求地址。
<强>数据:强>待发送键/值参数。
<强>回调:>强发送成功时回调函数。
<>强类型:>强返回内容格式、xml、html、脚本,json,文本,_default .
<强>套用格式:强>
$ . post (“test.php",,功能(数据){ 警报才能(“Data 加载:,“,+,数据); ,}); $ . get(“评论/getComments ? parentId=? parentId +“, topicId=? topicId功能(数据){ ,var appendButton =?”; ,var append =,““ ,如果(数据!=啊?{ var 才能;arr =, data.split(“美元“); var 才能;allTr=啊? ,,(var 小姐:=,0;i& lt; tr> & lt; td>“+内容+“& lt;/td> & lt;/tr> & lt; tr> & lt; td>“+时间+“& lt;/td> & lt;/tr> & lt;/table>“; appendButton 才能=,appendButton +表+“& lt; button type =, & # 39;按钮# 39;,id =, & # 39; toAddCommentId& # 39;, onclick =, & # 39; replaceFrom (“+ parentId +“, \““+名称+“\”;“+“)& # 39;在回复& lt;/button>“; ,,} appendButton 才能=,appendButton +“& lt; button type =, & # 39;按钮# 39;,onclick =, & # 39; replaceFrom (“+ parentId +“,“+“\”;“+用户名+“\”;“+“)& # 39;在我也说一句& lt;/button>“; ,}=,,appendButton  appendButton +“& lt; div id =, & # 39; commentButton& # 39;,祝辞& lt;/div> & lt; div id =, & # 39; textareaId& # 39;祝辞& lt;/div>“; ,如果(data=https://www.yisu.com/zixun/=" ") { appendButton=appendButton +“
<强>后台:强>
@RequestMapping (=value “/saveAndGetComments",, params =, {“topicId",“parentId"},, method =, RequestMethod.POST) ,@ResponseBody ,public String  saveAndGetComments (long  long topicId Comment 评论;parentId), throws UnsupportedEncodingException { ,comment.setParentId (parentId); ,commentService.save(评论,topicId); ,List评论=commentService.listByCommentId (parentId); ,return 附加(评论); ,} , ,private String 附加(List <强>注意>强,用springmvc3的注解@responseBody来传递参数。
<强>经常用到的js函数:强>
上面由于使用json来传递的数据,而js解析json传过来的日期时,不是我们想要的格式,这时需要对日期进行操作:
首先传过去的日期将它设为时间传过去date.getTime ()
<强>然后再在js中操作:强>
var 日期=,“/日期(“+时间+“)/? 时间=date DateFormat(日期);/* * ,*处理时间 ,* @param 价值 ,* @returns {字符串} ,*/function DateFormat(值),{ var 才能;date =, new 日期(方法(value.replace(“/日期(“,,,,).replace (“)/?,,,),, 10)); var 才能;month =, date.getMonth (), +, 1, & lt;, 10, ?,“0”, +, (date.getMonth (), +, - 1),:, date.getMonth (), +, 1; null null null null null null nullajax + springmvc如何实现C与观点之间的数据交流方法