本文介绍了在所请求的资源上没有“Access-Control-Allow-Origin”头。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用SpringMVC.我想调用一个XML文件与Web服务为了解析它以后。问题是,我无法访问XML文件,我有这个错误:没有访问控制-Allow-Origin'头部存在于请求的资源上。我尝试了以下解决方案: 我创建了一个新类,目的是添加Access- - 请求的ressource上的-Origin头。这是类 package com.mycompany.myapp; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class JsonpFilter implements Filter { private String functionName; @Override public void destroy(){ } @Override public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain chain)throws IOException,ServletException { if(!(request instanceof HttpServletRequest)){ throw new ServletException(This filter can +只处理HttpServletRequest请求); } HttpServletRequest httpRequest =(HttpServletRequest)request; HttpServletResponse response =(HttpServletResponse)servletResponse; if(isJSONPRequest(httpRequest)){ ServletOutputStream out = response.getOutputStream(); out.println(getCallbackMethod(httpRequest)+(); chain.doFilter(request,response); out.println();); response.setContentType(text / javascript); } else { response.addHeader(Access-Control-Allow-Origin,*); chain.doFilter(request,response); } } @Override public void init(FilterConfig filterConfig)throws ServletException { this.functionName = filterConfig.getInitParameter encoding); if(this.functionName == null || this.functionName.length()< = 0){ this.functionName =callback; } } private String getCallbackMethod(HttpServletRequest httpRequest){ return httpRequest.getParameter(this.functionName); } private boolean isJSONPRequest(HttpServletRequest httpRequest){ String callbackMethod = getCallbackMethod(httpRequest); return(callbackMethod!= null&& callbackMethod.length()> 0); } } < display-name> DataServices< / display-name> ; < filter> < filter-name> JSONPRequestFilter< / filter-name> < filter-class> com.mycompany.myapp.JsonpFilter< / filter-class> < init-param> < param-name> functionName< / param-name> < param-value> callback< / param-value> < / init-param> < / filter> < filter-mapping> < filter-name> JSONPRequestFilter< / filter-name> < url-pattern> / *< / url-pattern> < / filter-mapping> 我没有任何编译问题,但我仍然有同样的错误在控制台执行时间。所以,不考虑'Access-Control-Allow-Origin'头。 如果你能找到我的程序有什么问题,或者建议我另一个解决方案,我会非常感谢。解决方案要实现此接口here is an example@Componentpublic class CORSInterceptor implements HandlerInterceptor{ private static final Log LOG = LogFactory.getLog(CORSInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { LOG.trace("sending headers"); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { LOG.trace("afterCompletion is called"); }}Then add this line to your application context<mvc:interceptors> <bean class="com.elm.mb.rest.interceptors.CORSInterceptor" /></mvc:interceptors> 这篇关于在所请求的资源上没有“Access-Control-Allow-Origin”头。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-25 10:13