403 forbidden Rest API (node js) in Angular js1
403 forbidden Rest API (node js) in Angular js1
我正在将不同本地主机上的 nodeJS REST API 运行 调用到 angular 应用程序中,
在邮递员中,我得到了预期的结果。但在 angular 应用程序中,它由于 403 错误而下降,这是由于预检而发生的(预检响应具有无效的 HTTP 状态代码 403)。我尝试使用 $httpprovider 删除一些默认 headers 并且还尝试在 access-control-allow-region 中使用正确的服务器名称而不是通配符 (*) nothing worked.Looking 寻求帮助!
预检请求没有身份验证。
发布我的 req-respo 快照
通过在服务器端处理 OPTIONS 请求,我能够解决问题,
This post solved my problem
通过添加以下代码行
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
}
});
我正在将不同本地主机上的 nodeJS REST API 运行 调用到 angular 应用程序中,
在邮递员中,我得到了预期的结果。但在 angular 应用程序中,它由于 403 错误而下降,这是由于预检而发生的(预检响应具有无效的 HTTP 状态代码 403)。我尝试使用 $httpprovider 删除一些默认 headers 并且还尝试在 access-control-allow-region 中使用正确的服务器名称而不是通配符 (*) nothing worked.Looking 寻求帮助!
预检请求没有身份验证。
发布我的 req-respo 快照
通过在服务器端处理 OPTIONS 请求,我能够解决问题, This post solved my problem
通过添加以下代码行
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
}
});