Home >Web Front-end >JS Tutorial >A brief discussion on the problem of obtaining ModelAndView value in js

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

亚连
亚连Original
2018-05-28 16:13:171774browse

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 = &#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>

##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 + ")");

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!

Statement:
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