当前位置: 首页 > 面试题库 >

Angular,CORS和Spring的问题

凌鹏程
2023-03-14
问题内容

我有一个基于@RestController和AngularJS的简单项目。我可以将Angular的GET请求发送到@RestController,但无法发送POST请求。我在浏览器控制台中出错:

XMLHttpRequest无法加载http:// localhost:8080 /
add
。Access-Control-Allow-
Headers不允许请求标头字段Content-Type。

我的@RestController:

@RestController
public class AngularController {
    //@Autowired
  //  PhraseService phraseService;
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void add(@RequestBody String str){
        logger.info("Added");
        logger.info(str);

    }

    @RequestMapping("/")
    public void angularController(){;
        logger.info("Request!");
    }
}

这是我的CORS过滤器:

@Component
public class SimpleCORSFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        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", "x-requested-with");
        response.setHeader("Content-Type", "application/json");
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

我的AngularJS控制器:

function addController($scope, $http){
    $scope.url = 'http://localhost:8080/add';
    $scope.addPhrase = function(){
        $http.post($scope.url, {"data": $scope.value});
    }
}

和index.html:

<!doctype html>
<html ng-app>
<head>
    <title>Hello AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
    <script src="server.js"></script>
</head>

<body>
<div ng-controller="addController">
    <form>
    <input type="text" np-model="value"/>
        <button type="button" ng-click="addPhrase()">Send!</button>
    </form>
</div>
</body>
</html>

我试图用标题:“ Content-Type”:“ application / json”解决此问题,但是它给了我错误的请求错误。


问题答案:

您还可以考虑使用即将发布的Spring Framework 4.2版本中提供的本地CORS支持。有关更多详细信息,请参见此博客文章。

默认情况下,它配置为自动接受所有标头,因此,一旦启用带有CrossOrigin注释或Java配置的CORS支持,它便应立即起作用。



 类似资料:
  • 我正在尝试用Spring Boot为后端和Angular 2为前端研发App。 实际上,我有连接问题,当我从Spring访问mvc登录页面。 我遇到以下问题: (控制台) 我为网页前端工程师端口设置了全局Cors配置。我的请求返回响应状态200。但是我无法访问响应,因为我在控制台中收到错误消息。 Spring没有误差。

  • 嗨,我的微服务sso设置有CORS问题,但我无法找出原因。我的设置: OAuth微服务端口8899: 主控制器 zuul网关端口8765: zuul配置: 角2应用程序端口4200网关后面: 服务 组件 因此,如果我转到浏览器并打开localhost:8765/request,它将以根目录形式指向执行getLoggedInUser()调用的主angular2页面。这将转到“localhost:87

  • 我在Stack Overflow上也看到过其他类似的问题,但到目前为止,还没有什么能帮到我。 我有一个有棱角的应用程序http://localhost:4200.我在http://localhost:8080/myApp/ 我试图设置到达后端从我的前端与以下调用: API_URLhttp://localhost:8080/myApp 我的代理配置如下所示: 我的包裹。json具有以下特点: 我在尝

  • 我使用Spring Boot和使用Spring Cloud APIGW的微服务堆栈。我使用这里提到的相同代码:https://piotrminkowski.com/2020/02/20/microservices-api-documentation-with-springdoc-openapi/ 当我到达任何一个endpoint时,我都看不到响应正在到来并低于错误。 CORS 策略已阻止从源“ht

  • 我使用的是Spring Boot版本2.0.2Release。下面是我的安全配置 加载http://localhost:8080/myurl失败:对预飞行请求的响应未通过访问控制检查:请求的资源上没有“access-control-allow-origin”标头。因此,不允许访问源“http://localhost:4200”。响应的HTTP状态代码为403。

  • 我在spring cloud gateway应用程序中设置CORS配置时遇到了问题。我已经将以下配置设置为允许所有内容中的所有内容: 然而,当我调用endpoint到网关时,我会得到: null 以下是api gateway应用程序在调试时设置的日志(此处为完整日志):