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个回答

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐