请求头没有origin参数_在所请求的资源上没有“Access-Control-Allow-Origin”头。

I'm using SpringMVC.I want to call an XML file with web service in order to parse it later.The problem is that I can't access the XML file, I have got this error:No 'Access-Control-Allow-Origin' header is present on the requested resource.I have tried the solution below:

I created a new class which purpose is to add Access-Control-Allow-Origin' header on the requested ressource.This is the class

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 "

+ " only process HttpServletRequest requests");

}

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);

}

}

then I add the these two lines in my web.xml file:

DataServices

JSONPRequestFilter

com.mycompany.myapp.JsonpFilter

functionName

callback

JSONPRequestFilter

/*

I don't have any complilation problem, but I still have the same error in the console at the execution time.So, the 'Access-Control-Allow-Origin' header isn't taken into account.

Please,If you can find out what is wrong with my program, or suggest me another solution, I will be thankful

解决方案

To do this implement this interface

org.springframework.web.servlet.HandlerInterceptor

here is an example

@Component

public 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值