簡體   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