Enable CORS AngularJS to send HTTP POST request

后端 未结 5 979
眼角桃花
眼角桃花 2020-12-10 02:33

I want to send an HTTP POST request by submitting a form to my server, which is located at a different domain (enabled cors in the server script using node.js).

Thi

5条回答
  •  我在风中等你
    2020-12-10 02:48

    That's how I do CORS in express applications, you have to remember about OPTIONS because for some frameworks there are 2 calls for CORS, first one is OPTIONS which checks what methods are available and then there is actual call, OPTIONS require just empty answer 200 OK

    js

    allowCrossDomain = function(req, res, next) {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
      res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
      if ('OPTIONS' === req.method) {
        res.send(200);
      } else {
        next();
      }
    };
    
    app.use(allowCrossDomain);
    

提交回复
热议问题