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 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题