我嘗試發(fā)布一本包含預(yù)訂數(shù)據(jù)的字典。但是 chrome 記錄了這個(gè)錯(cuò)誤: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.這很奇怪,因?yàn)槲铱梢园l(fā)布圖像、視頻、html 內(nèi)容,因?yàn)槲褸CrossOrigin在控制器上方放置了注釋。但是對(duì)于這個(gè)特定的帖子請(qǐng)求,它似乎不起作用。休息控制器:@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貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果您只設(shè)置 allowedHeaders,您將允許此參數(shù),如果它收到其他參數(shù),它永遠(yuǎn)不會(huì)發(fā)送交叉原始標(biāo)頭,chrome 將拋出錯(cuò)誤。
如果不需要,則應(yīng)刪除 allowedHeaders、exposedHeaders 和 allowCredentials。
添加回答
舉報(bào)
0/150
提交
取消