dream_sky




  种一棵树最好的时间是  十年前。      其次是,     现在!

springmvc处理ajax跨域

解决跨域问题:
在web.xml中配置corsFilter



mvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!--  json配置 -->
     <!-- 用于将对象转换为 JSON  --> 
    <bean id="stringConverter"
        class="org.springframework.http.converter.StringHttpMessageConverter"
        <property name="supportedMediaTypes"
            <list> 
                <value>text/plain;charset=UTF-8</value> 
            </list> 
        </property> 
    </bean> 
    <bean id="jsonConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean> 
 
    <bean 
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
        <property name="messageConverters"
            <list> 
                <ref bean="stringConverter" /> 
                <ref bean="jsonConverter" /> 
            </list> 
        </property> 
    </bean>
<wiz_tmp_tag class="wiz-block-scroll">
 

 

controller:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RequestMapping("/ajax.do")
    public void ajax(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        resp.getWriter().print("ajax data");
    }
    @RequestMapping("/json.do")
    @ResponseBody              //返回json格式的数据。
    //将会把返回值 转换为json对象
    public List<User> json(){
        List<User> list = new ArrayList<User>();
        list.add(new User(1,"zhansan",22));
        list.add(new User(2,"wangwu",21));
        list.add(new User(3,"zhaosi",33));
        list.add(new User(4,"wangdana",14));
        return list;
    }

 

jsp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script type="text/javascript">
    $(function(){
        $('#btn').click(function(){
            $.post("ajax.do",function(data){
                $("#content").html(data);
            });
        });
    });
    </script>
 
  </head>
 
  <body>
   <input type="button" id="btn" value="ajax"/><br>
   <div id="content"></div>
  </body>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<script type="text/javascript">
    $(function(){
        $('#btn').click(function(){
            $.post("json.do",function(data){
            var html="";
                for(var i=0;i<data.length;i++){
                html+="<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].age+"</td></tr>"
                }
                $('#content').html(html);
            });
        });
    });
    </script>
 
  </head>
 
  <body>
   <input type="button" id="btn" value="获取数据"/><br>
   <table width="80%" align="center">
    <tr>
        <td>编号</td>
        <td>姓名</td>
        <td>年龄</td>
    </tr>
    <tbody id="content"></tbody>

 

posted @ 2018-06-14 16:09  书晨007  阅读(295)  评论(0编辑  收藏  举报
将时间精确到分,将获得较高的效率!