Home >Web Front-end >JS Tutorial >A brief discussion on the problem of obtaining ModelAndView value in js
Below I will share with you a brief discussion on the problem of obtaining the ModelAndView value with js. It has a good reference value and I hope it will be helpful to everyone.
Can't the return value of ModelAndView be received in JS? Does it have to be received in a JSP page?
1 Method 1 [valid]
#Yes, it is the same as the el expression access method .
Sample code, a userId is stored in the Action of a data display request:
@RequestMapping(value="/diary") public ModelAndView toDiaryList(HttpSession session){ ModelAndView view = new ModelAndView("/diary_list"); TbUser user = (TbUser)session.getAttribute(SystemConstant.CURRENT_USER); //set info of current user if(user!=null){ Integer id = user.getId(); view.addObject("userId",id); } return view; }
in the js in the page jsp file Use this userId as the query condition:
<script type="text/javascript"> var path = '<%=basePath%>'; var author=${userId}; $(document).ready(function(){ queryList(); }); function queryList(){ $.ajax({ type : 'POST', url : path+'queryDiaryList', //通过url传递name参数 data : { author:author, page:_currentPage, pageSize:_pageSize, type:$("#queryType").val() }, dataType : 'json', success:function(data){ if(data.status){ showTable(data.result); //调用分页插件,初始化分页p pageShow("queryList",data.ext.total); }else{ alert(data.description); } }, error:function(e){ alert("Net error ,try later."); } }); } </script>
##2 Method 2
【efficient? 】Is the platform returning js or json? This must be clarified! Assume that the string returned by the background is stored in responseText, then If it is js, thenvar result = eval("(" + responseText + ")");If it is json, just
var result = JSON.parse(responseText);
3 Method three [valid]
Add hidden fields,<input id="autoflag" type="hidden" value="${autoflag}">Easy for js to read
var passflag=document.getElementById("autoflag");The above is what I compiled for everyone, I hope it will be helpful to everyone in the future. Related articles:
Ajax core XMLHttpRequest summary
Solving the problem of ajax cross-domain request data cookie loss
Use ajax to change page content and address bar URL without refreshing
##
The above is the detailed content of A brief discussion on the problem of obtaining ModelAndView value in js. For more information, please follow other related articles on the PHP Chinese website!