Java 在Vuejs中使用axios时,如何避免跨源资源共享(CORS)?

Java 在Vuejs中使用axios时,如何避免跨源资源共享(CORS)?,java,web-services,vuejs2,axios,Java,Web Services,Vuejs2,Axios,我使用Vue js开发了一个web应用程序。我正在使用axios进行API调用。我正在使用Web服务进行API。我已将CORS过滤器包含在我的API中,用于“允许控制允许来源”。 我使用的axios如下: axios.get('http://192.26.3.51:8097/FeedbackPhase2/login) .then(response => { this.fb1 = response.data this.vh = this.fb1.length }) Failed to lo

我使用Vue js开发了一个web应用程序。我正在使用axios进行API调用。我正在使用Web服务进行API。我已将CORS过滤器包含在我的API中,用于“允许控制允许来源”。 我使用的axios如下:

axios.get('http://192.26.3.51:8097/FeedbackPhase2/login) .then(response => { this.fb1 = response.data this.vh = this.fb1.length })
Failed to load http://192.26.3.51:8097/FeedbackPhase2/login: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
在我的CORS过滤器中:

@Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

        chain.doFilter(req, res);
    }
问题是,当我在不同的浏览器中加载页面时,会出现如下错误:

axios.get('http://192.26.3.51:8097/FeedbackPhase2/login) .then(response => { this.fb1 = response.data this.vh = this.fb1.length })
Failed to load http://192.26.3.51:8097/FeedbackPhase2/login: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.


我该怎么办?请指导我。

如果您使用
fetch命令,您会得到什么http://192.26.3.51:8097/FeedbackPhase2/login然后(response=>{this.fb1=response.data this.vh=this.fb1.length})相反?我得到的是“TypeError:NetworkError when event fetch resource.”,与“未能加载:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“access control Allow Origin”标头。因此,不允许访问源“”