1. 程式人生 > 其它 >Bug記錄-Request header field token is not allowed by Access-Control-Allow-Headers...

Bug記錄-Request header field token is not allowed by Access-Control-Allow-Headers...

技術標籤:JavaBugjavaCORS跨域token請求頭

場景:

請求頭設定token後頁面報錯:has been blocked by CORS policy:Request header field token is not allowed by Access-Control-Allow-Headers in preflight response.

並且後臺跨域相關已配置:Access-Control-Allow-Origin: *

解決:

需要後端在響應頭的 Access-Control-Allow-Headers 欄位設定允許前端請求時帶有的欄位值(檢查了一下,在攔截器的響應裡裡增加如下)

response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, accessToken, token");