我正在使用AngularJS和Spring Boot创建REST应用程序。实际上,我正在尝试测试我的应用程序,我有疑问。
我可以毫无问题地在localhost上运行前端和后端吗?
我在Chrome控制台中询问我是否收到错误:
angular.js:11028 XMLHttpRequest无法加载 本地主机:8080 /文件/上传。仅支持跨源请求 协议方案:http,数据,chrome,chrome-extension,https, 铬扩展资源。
但我在Spring应用程序中配置了CORS。此外,我尝试使用Chrome扩展程序“allow-controll-allow-origin:*”,但仍无效。
My Spring配置:
package com.example.appconfig;
import org.slf4j.Logger;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
private final Logger log = org.slf4j.LoggerFactory.getLogger(CorsFilter.class);
public CorsFilter() {
}
@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-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);
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
PS我的AngularJS应用程序我上次使用不同的后端测试过,我没有遇到任何问题。
感谢您的帮助。