Home > Web Front-end > JS Tutorial > body text

A brief discussion on the problem of obtaining ModelAndView value in js

亚连
Release: 2018-05-28 16:13:17
Original
1745 people have browsed it

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;
  }
Copy after login

in the js in the page jsp file Use this userId as the query condition:

<script type="text/javascript">
    var path = &#39;<%=basePath%>&#39;;
    var author=${userId};
    $(document).ready(function(){
      queryList();
    });
    function queryList(){
      $.ajax({
        type : &#39;POST&#39;,
        url : path+&#39;queryDiaryList&#39;, //通过url传递name参数
        data : {
            author:author,
            page:_currentPage,
            pageSize:_pageSize,
            type:$("#queryType").val()
          },
        dataType : &#39;json&#39;,
        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>
Copy after login

##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, then

var result = eval("(" + responseText + ")");
Copy after login

If it is json, just

var result = JSON.parse(responseText);
Copy after login

3 Method three [valid]

Add hidden fields,

<input id="autoflag" type="hidden" value="${autoflag}">
Copy after login

Easy for js to read

var passflag=document.getElementById("autoflag");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template