node.js - Angular2 和 Node : Response for preflight has invalid HTTP status code 403

标签 node.js angular http-headers content-type angular2-http

我在 Angular 2 中有以下 http 方法,其 header 设置为 {"Content-Type": "application/json",'Authorization':this.authToken}

public allJobsStageWidget1= (): Observable<WidgetModel[]> => {
    let widget1Url=  this._configuration.ServerWithApiUrl + 'getAllJobsStagesCount';
    return this._http.get(widget1Url,new RequestOptions({
      headers: new Headers({"Content-Type": "application/json",'Authorization':this.authToken})
    }))
      .map((response: Response) => {console.log(response.json());response.json() })
      .catch(this.handleError);
  }

在我的 Node 应用程序中,完成了以下操作:

app.use(function(req, res, next) {
   res.header('Access-Control-Allow-Credentials', true);
   res.header('Access-Control-Allow-Origin', '*');
   res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
   res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
   res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Authorization ,Accept');
   next();
});

当我发出 GET 请求时,'XMLHttpRequest 无法加载 http://rest-0023:3000/api/getAllJobsStagesCount。预检响应具有无效的 HTTP 状态代码 403' 错误,因为我已经设置了所有 header 。但我不知道我到底在哪里遗漏了东西。

最佳答案

您似乎没有拒绝授权方法。我认为标题行应该如下所示:

headers: new Headers({
   "Content-Type": "application/json",
   "Authorization": "Basic " + this.authToken
})

关于node.js - Angular2 和 Node : Response for preflight has invalid HTTP status code 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39039606/

相关文章:

asp.net - 通过配置向 Cassini 添加 http header

java 如何发送带有 header 的 get 请求?

node.js - npm install ionic 后 Ionic 无法启动

node.js - 从 Express 堆栈中删除中间件的正确方法?

node.js - Protractor :text.indexOf(...).isDisplayed 不是函数

angular - 如何从 http 请求将数据传递给 ng2-smart-table renderComponent?

javascript - 错误 NG8001 : 'router-outlet' is not a known element

php - 如何将位置 header 添加到来自 Codeigniter Rest 服务器的 http 响应

node.js - 如何在 Hudson 中显示 Expresso 和 node-jscoverage 输出?

html - Angular 2 从 URL 中删除哈希 (#)