0

I am calling nodeJS REST API running on different localhost into an angular app, in postman I get the expected result. but in angular application it is falling due to 403 error, It is happening because of preflight( Response for preflight has invalid HTTP status code 403). I tried to remove some default headers using $httpprovider and also tried to use proper server name instead of wildcard(*) in access-control-allow-region nothing worked.Looking for help! There is no Authentication for preflight requests. Posting my req-respo snapReq-Responce snap

Pratik Thube
  • 124
  • 2
  • 9
  • Possible duplicate of [XMLHttpRequest cannot load .... Response for preflight has invalid HTTP status code 401](https://stackoverflow.com/questions/38326128/xmlhttprequest-cannot-load-response-for-preflight-has-invalid-http-status-c) – sp00m Sep 29 '17 at 11:06
  • 2
    If you trying to resolve it using Angular, nothing going to help you. It need to fixed server side. It is `CORS` issue.` – Ved Sep 29 '17 at 11:07
  • thanks @Ved,Vijayanath Viswanathan,sp00m – Pratik Thube Sep 29 '17 at 12:05
  • @ved,Solved it fixed after adding lines of code at server side. – Pratik Thube Sep 29 '17 at 12:12

1 Answers1

0

By handling OPTIONS request at server side i am able to solve the problem, This post solved my problem

By adding following line of code

app.use(function (req,res,next){
     if (req.method === 'OPTIONS') {
          console.log('!OPTIONS');
          var headers = {};
          // IE8 does not allow domains to be specified, just the *
          // headers["Access-Control-Allow-Origin"] = req.headers.origin;
          headers["Access-Control-Allow-Origin"] = "*";
          headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
          headers["Access-Control-Allow-Credentials"] = false;
          headers["Access-Control-Max-Age"] = '86400'; // 24 hours
          headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
          res.writeHead(200, headers);
          res.end();
    } else {
    //...
    other requests
    }
});
Pratik Thube
  • 124
  • 2
  • 9