CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true

后端 未结 9 2509
借酒劲吻你
借酒劲吻你 2020-11-22 01:54

I have a setup involving

Frontend server (Node.js, domain: localhost:3000) <---> Backend (Django, Ajax, domain: localhost:8000)

Browser <-- webapp <

9条回答
  •  -上瘾入骨i
    2020-11-22 02:58

    Expanding on @Renaud idea, cors now provides a very easy way of doing this:

    From cors official documentation found here:

    " origin: Configures the Access-Control-Allow-Origin CORS header. Possible values: Boolean - set origin to true to reflect the request origin, as defined by req.header('Origin'), or set it to false to disable CORS. "

    Hence we simply do the following:

    const app = express();
    const corsConfig = {
        credentials: true,
        origin: true,
    };
    app.use(cors(corsConfig));
    

    Lastly I think it is worth mentioning that there are use cases where we would want to allow cross origin requests from anyone; for example, when building a public REST API.

    NOTE: I would have liked to leave this as a comment on his answer, but unfortunately I don't have the reputation points.

提交回复
热议问题