springmvc sends and receives data by ajax request using json format
生活随笔
收集整理的這篇文章主要介紹了
springmvc sends and receives data by ajax request using json format
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【0】README
1) springmvc sends or receives data by ajax request using json format;
【1】 ajax function
<script type="text/javascript">var checkoutUserlistIsAccess = false;$(document).ready(function(){ $("#first").hide();if(checkoutUserlistIsAccess==false) {checkoutUserlist(); // checkout user list.}/* bind event to input with id searched *//* $("#search").bind("click", function() {$("#second").hide();$("div[id^='first']").hide();});$("#search").bind("blur", function() {$("#second").show();$("div[id^='first']").hide();}); */});//ajax 訪問函數var member;function checkoutUserlist(){checkoutUserlistIsAccess = true;var userid=1;// alert("request for user list by ajax.");var url = "<c:url value='/chat/userlist' />"; //請求的地址 $.post(url,{keyword:userid //[逗號 連接 ]},function(data){ // 回調函數 .member = data;for(var i=0; i<data.length; i++) {appendAIntoDiv("second_userlist", data[i]);}},"json"); }// append <a> into a div.function appendAIntoDiv(objId, value) {$("#"+objId).append("<a href='<c:url value='/chat/single?touser=" + value + "'/>' class='list-group-item'>????" + value +"</a>");} </script>
【2】method?sending data via json format defined in springmvc controller
// ajax requests for user list (by json format).@RequestMapping(value="/userlist", method=RequestMethod.POST, produces="application/json")public @ResponseBody String checkoutUserlist() {List<String> list = repository.checkoutUserlist();ObjectMapper mapper = new ObjectMapper();try {String json_result = mapper.writeValueAsString(list);System.out.println(json_result);return json_result;} catch (JsonProcessingException e) {e.printStackTrace();return null;}}
【3】method?receiving data via json format defined in springmvc controller
總結
以上是生活随笔為你收集整理的springmvc sends and receives data by ajax request using json format的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于 tomcat启动后无法访问的问题(
- 下一篇: 霞最新符文天赋(霞符文2020)