How to Enable CORS from Nodejs server

前端 未结 3 1571
感动是毒
感动是毒 2021-01-22 21:19

I am using react to send data to my API. Every POST request I make gives me an OPTIONS request, and I need to fix this. I think I might need to do some preflight structure but a

3条回答
  •  猫巷女王i
    2021-01-22 21:57

    The browser sends a preflight Options requests to the server to check if CORS is enabled on the server. To enable cors on the server side add this to your server code

    app.use(function(req, res, next) {
            res.header("Access-Control-Allow-Origin", "*");
            res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
            next();
        });
    

提交回复
热议问题