简体   繁体   English

Angular js1中的403禁止的Rest API(node js)

[英]403 forbidden Rest API (node js) in Angular js1

I am calling nodeJS REST API running on different localhost into an angular app, in postman I get the expected result. 我将在不同本地主机上运行的nodeJS REST API调用到有角度的应用程序中,在邮递员中,我得到了预期的结果。 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). 但是在有角度的应用程序中,它由于403错误而掉落,这是由于预检而发生的(对预检的响应具有无效的HTTP状态代码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! 我试图使用$ httpprovider删除一些默认标头,并且还尝试在access-control-allow-region中使用正确的服务器名称而不是通配符(*),但是没有用。 There is no Authentication for preflight requests. 飞行前请求没有身份验证。 Posting my req-respo snap 发布我的req-repo快照 需求响应捕捉

By handling OPTIONS request at server side i am able to solve the problem, This post solved my problem 通过在服务器端处理OPTIONS请求,我可以解决问题, 本文解决了我的问题

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
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM