Missing token in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel

后端 未结 1 437
囚心锁ツ
囚心锁ツ 2020-12-06 12:53

Backend returns

Access-Control-Allow-Headers: *

I have a request like

fetch(\'url-here\', {
    // ...
    headers: {
             


        
相关标签:
1条回答
  • 2020-12-06 13:26

    The problem is, some browsers don’t yet allow * wildcards for Access-Control-Allow-Headers. Notably, Firefox 69 and earlier doesn’t. See https://bugzilla.mozilla.org/show_bug.cgi?id=1309358.

    So to ensure you get expected behavior in all browsers, the Access-Control-Allow-Headers value you send back should explicitly list all the header names you actually need to access from your frontend code; e.g., for the case in the question: Access-Control-Allow-Headers: X-Auth.

    One way you can make that happen without needing to hardcode all the header names is: Have your server-side code take the value of the Access-Control-Request-Headers request header the browser sends, and just echo that into the value of the Access-Control-Allow-Headers response header your server sends back.

    Or else use some existing library to CORS-enable your server. Echoing the Access-Control-Request-Headers request-header value into the Access-Control-Allow-Headers response-header value is something most CORS libraries will typically do for you.

    0 讨论(0)
提交回复
热议问题