繁体   English   中英

Spring MVC & Angular - 原因:缺少 CORS 标头“Access-Control-Allow-Origin”

[英]Spring MVC & Angular - Reason: CORS header ‘Access-Control-Allow-Origin’ missing

当我尝试设置Content-Type:application/json出现 CORS 错误原因:CORS header 'Access-Control-Allow-Origin' missing

这是我在 Spring MVC 中的 Cors 覆盖:

@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowCredentials(true).allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS").allowedHeaders("*").allowedOrigins("http://localhost:4200");
}

我的帖子API:

@RequestMapping(value = "/seanPost", method = RequestMethod.POST)
public  String post(HttpServletRequest request, @RequestBody TrackFileStatusFindForm form ) {

我在 Angular 中的请求:

options = {
headers: new HttpHeaders()
    .set('Accept', 'application/json')
    .set('Content-Type', 'application/json')
};

this.http.post(this.APP_URL + '/seanPost', JSON.stringify(this.model), this.options)
.subscribe(
    data => {
        this.postId = data;
        console.log(this.postId);
    },
    error => {
    console.log('Error occured', error);
  }
) 

我的 Spring 项目在本地部署在 WebLogic 12c 上,如果有帮助,我正在使用 https。

谢谢

试试这个:

private httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM