Node.js : POST - Request Method: OPTIONS Status Code: 403 Forbidden

后端 未结 3 2054
时光说笑
时光说笑 2021-01-17 19:39

We have the following setup :

Front end code : REACT (Hosted using express js) (lets call this www.domainA.com)
Backend        : .NET WEB API (Hosted in IIS          


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-17 20:27

    The answer from Jannes Botis explains well the Preflighted mechanism. I'm just adding the code I'm using to solve this issue on Node.js / Express

    const express = require('express');
    const app = express();
    
    app.use((req, res, next) => {
      res.setHeader('Access-Control-Allow-Origin', 'http://www.domainA.com');
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS');
      res.setHeader(
        'Access-Control-Allow-Headers',
        'Origin, X-Requested-With, Content-Type, Accept, Authorization'
      );
      next();
    });
    
    // All OPTIONS requests return a simple status: 'OK'
    app.options('*', (req, res) => {
      res.json({
        status: 'OK'
      });
    });
    
    app.get('/', ...);
    
    app.post('/api/postdataoperation', ...);
    

提交回复
热议问题