caisoft16 2017-11-07 07:51 采纳率: 0%
浏览 2008

Spring 跨域请求设置head无效

Filter中的设置如下:
@Override
public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse httpResponse = (HttpServletResponse) res;
String method = request.getMethod().toLowerCase();
String path = request.getRequestURI();
logger.info("uri:" + path);
path = path.substring(path.lastIndexOf("/") + 1);
logger.info("path:" + path);
String path1 = request.getPathTranslated();
logger.info("url:" + request.getRequestURL());
logger.info("method:" + method);
logger.info("context path:" + request.getContextPath());
String curOrigin = request.getHeader("Origin");
System.out.println("当前访问来源是:" + curOrigin);

httpResponse.setHeader("Access-Control-Allow-Origin", "null"); //解决跨域访问报错

httpResponse.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");

httpResponse.setHeader("Access-Control-Max-Age", "3600"); //设置过期时间

httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, client_id, uuid, Authorization");

httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支持HTTP 1.1.

httpResponse.setHeader("Pragma", "no-cache"); // 支持HTTP 1.0. response.setHeader("Expires", "0");

httpResponse.setHeader("Access-Control-Allow-Credentials","true");

chain.doFilter(req, res);

}

前端代码如下:
<!DOCTYPE html>







    <span id="as"></span>
    <script type="text/javascript">
        var encoded_uri = encodeURI("http://www.hl-epay.com/cscmd/applygasmoney.do"); 
            console.log(encoded_uri)
        $(function($){  
          $.ajax("http://www.hl-epay.com/cscmd/applygasmoney.do", { 
            type:'get',
            data: {  
              'icno':'123456',  
            },  
            headers:{               
            'Content-Type':'application/json'
            },
            dataType: 'json', 
           crossDomain:'true',  
            success: function(data){  
            var data=JSON.stringify(data)
            //var data = $.parseJSON(data);
             console.log('成功'+data)
            },
            error:function(err){
                var err=JSON.stringify(err)
                console.log("错误"+err)
            }
        }); 
  }); 

    </script>
</body>

访问始终显示:
demodate.html:13 http://www.hl-epay.com/cscmd/applygasmoney.do
demodate.html:1 Failed to load http://www.hl-epay.com/cscmd/applygasmoney.do?icno=123456: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
demodate.html:32 错误{"readyState":0,"status":0,"statusText":"error"}

  • 写回答

1条回答

  • caisoft16 2017-11-07 07:53
    关注

    从本地访问,Origin: 为null, 从其他的机器访问:显示具体机器的IP地址

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?