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>

     

  • 相关阅读:
    de1ctf_2019_weapon
    ciscn_2019_s_6
    用 Blend 给Windows Phone 应用创建 示例数据
    (转) Wp7 list 中列表项多样化的解决方案-Custom DataTemplateSelector
    Android开发之Java集合类性能分析
    WriteableBitmap 给透明的控件截图的问题
    VS 2013 配置OpenCV
    Cocos2d-x 版本小游戏 《是男人就下100层》 项目开源
    HEAP CORRUPTION DETECTED :after Normal block 错误
    Visual C++ 的代码折叠
  • 原文地址:https://www.cnblogs.com/shuchen007/p/9183546.html
Copyright © 2011-2022 走看看