zoukankan      html  css  js  c++  java
  • 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>

     

  • 相关阅读:
    Adobe Edge Animate –EdgeCommons Log和全局变量设置功能
    Adobe Edge Animate –使用EdgeCommons加载和播放音频
    Adobe Edge Animate –svg地图交互-精确的边缘及颜色置换
    Adobe Edge Animate –解决图形边缘精确检测问题-通过jquery加载svg图片
    Adobe Edge Animate –修改Edge Commons Spotlight功能,使之能支持播放中国网站视频
    Adobe Edge Animate –获取鼠标位置及跟随鼠标功能实现
    Adobe Edge Animate –使用css制作菜单
    Adobe Edge Animate –Edge Commons强势来袭,Edge团队开发成为现实
    Adobe Edge Animate –可重复使用的个性化按钮制作
    Adobe Edge Animate –弹性的方块-使用tweenmax缓动效果
  • 原文地址:https://www.cnblogs.com/shuchen007/p/9183546.html
Copyright © 2011-2022 走看看