gpt4 book ai didi

angularjs - 如何调试 "No ' Access-Control-Allow-Origin' header 出现在请求的资源上”

转载 作者:行者123 更新时间:2023-12-02 23:00:05 24 4
gpt4 key购买 nike

我在浏览器控制台上显示此错误:

XMLHttpRequest cannot load http://localhost:8080/api/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9009' is therefore not allowed access.

我使用的环境是:

  • 后端- Spring Boot
  • 前端- Angularjs
  • 网络服务器- Grunt

在服务器上,我已经将请求和响应中的 header 定义为:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;

httpResponse.setHeader("Access-Control-Allow-Origin", "*");
httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,PUT");
httpResponse.setHeader("Access-Control-Max-Age", "3600");
httpResponse.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization, Content-Type");

if (httpRequest.getMethod().equals("OPTIONS")) {
httpResponse.setStatus(HttpServletResponse.SC_ACCEPTED);
return;
}
}

我已经在此链接 No 'Access-Control-Allow-Origin' header is present on the requested resource 上找到了这个问题但找不到合适的解决方案。

这是浏览器网络图像:

enter image description here

最佳答案

您的服务器应该为预检/实际 CORS 请求添加正确的 CORS header 。我不建议实现您自己的过滤器,因为这是一个相当复杂的规范。

从 4.2 开始,Spring Framework 通过全局配置或过滤器支持 CORS - see this blog post for more information .

如果您使用的是 Spring Boot,版本 1.3.0(即将发布)will integrate this support .

关于angularjs - 如何调试 "No ' Access-Control-Allow-Origin' header 出现在请求的资源上”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33101918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com