ajax访问非同域名下的跨域问题

这里使用的Jquery版本是

jquery-1.4.2.js
各种jquery版本下载地址:
  http://www.jq22.com/jquery-info122

//前端代码:

  

$.ajax({
                url: localUrl + "insur/setInsuRisk",
                type: 'POST',
                async: true,
                dataType: 'json',
                data: {insuCode:insurCode},
                success: function (data) {
                    if ("0" == data.code) {
                        //获取险种代号的id
                        $("#insurId").val(data.data.info.id);
                        //绑定险种信息
                        // $("#isInsured").append("<option >" + data.data.info.isInsured + "</option>");
                        $('select#isInsured').children('option').each(function () {
                            if ($(this).val() == data.data.info.isInsured) {
                                $(this).attr('selected', true);
                            }
                        });

                        $('select#calcType').children('option').each(function () {
                            if ($(this).val() == data.data.info.calcType) {
                                $(this).attr('selected', true);
                            }
                        });
                        // $("#calcType").empty();
                        // $("#calcType").append("<option >" + data.data.info.calcType + "</option>");
                        //绑定风险组列表信息
                        var str = "<tr><td width='40%'class='tbBlue'>风险项</td><td width='30%' class='tbBlue'>保额系数</td><td width='30%' class='tbBlue'>操作</td></tr>";
                        for (i in data.data.riskList) {
                            str +=
                                "<tr>" + "<td width='40%' class='tbBlue3'>" + data.data.riskList[i].riskDesc + "</td>" +
                                "<td width='30%' class='tbBlue3'>" + data.data.riskList[i].ratio + "</td>" +
                                "<td width='30%' class='tbBlue3'>" + "<input type=\"button\" class='button' value=\"删除\" onclick=\"deleteRiskRatio(\'" + data.data.riskList[i].ratioId + "\',\'" + insurCode + "\')\"/>" +
                                "</td>" + "</tr>"
                        }
                        $("#ratioTable").html(str);

                        //绑定风险项目固定值
                        var strSelect = "";
                        for (i in data.data.list) {
                            strSelect += "<option value='" + data.data.list[i].riskItem + "'>" + data.data.list[i].riskDesc + "</option>"
                        }
                        $("#ratioSelect").html(strSelect);
                    } else {
                        alert(data.message);
                        window.location.reload();
                    }

                },error:function () {
                    alert("cuow");
                }
            });

 

//后端代码:

  

  1.在Controller层注入CrossOrigin

  2.配置跨域类

  

  3.在web.xml配置路径

  

 1 import org.springframework.web.filter.OncePerRequestFilter;
 2 
 3 import javax.servlet.FilterChain;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.IOException;
 8 
 9 //配置解决跨域类
10 public class CORSFilteUtil extends OncePerRequestFilter {
11     @Override
12     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
13         response.setHeader("Access-Control-Allow-Credentials", "true");
14 
15         response.addHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
16         response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
17         response.addHeader("Access-Control-Allow-Headers", "Content-Type");
18         response.addHeader("Access-Control-Max-Age", "1800");//30 min
19         filterChain.doFilter(request, response);
20 
21     }
22 }

 

1 <!--配置前端ajax请求,解决跨域问题-->
2     <filter>
3         <filter-name>cros</filter-name>
4         <filter-class>com.cathay.riskamt2.util.CORSFilteUtil</filter-class>
5     </filter>
6     <filter-mapping>
7         <filter-name>cros</filter-name>
8         <url-pattern>/*</url-pattern>
9     </filter-mapping>

 

posted @ 2019-03-25 15:12  夜天晰  阅读(303)  评论(0编辑  收藏  举报