我嘗試發布一本包含預訂數據的字典。但是 chrome 記錄了這個錯誤:Access to XMLHttpRequest at 'http://localhost:8080/reservations' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.這很奇怪,因為我可以發布圖像、視頻、html 內容,因為我@CrossOrigin在控制器上方放置了注釋。但是對于這個特定的帖子請求,它似乎不起作用。休息控制器:@CrossOrigin(origins="http://localhost:4200", maxAge=2000,allowedHeaders="header1,header2", exposedHeaders="header1",allowCredentials= "false")@RestControllerpublic class ReservationsController { private ReservationDao dao; @Autowired public ReservationsController(ReservationDao dao) { this.dao = dao; } @PostMapping("/reservations") public Map<String, String> bookReservation(@RequestBody Map<String, String> reservation) { System.out.println(reservation); return null; }}angular api bookReservation方法: bookReservation(data) { console.log(data); const result = this.http.post(this.apiUrl + 'reservations', data).subscribe( (val) => { console.log('POST call succesful value returned in body', val); }, response => { console.log('POST call in error', response); }, () => { console.log('The POST observable is now completed'); }); console.log(result); }
1 回答

jeck貓
TA貢獻1909條經驗 獲得超7個贊
如果您只設置 allowedHeaders,您將允許此參數,如果它收到其他參數,它永遠不會發送交叉原始標頭,chrome 將拋出錯誤。
如果不需要,則應刪除 allowedHeaders、exposedHeaders 和 allowCredentials。
添加回答
舉報
0/150
提交
取消