【问题标题】:Angular2 and Node : Response for preflight has invalid HTTP status code 403Angular2 和节点:预检响应具有无效的 HTTP 状态代码 403
【发布时间】:2016-12-26 15:33:18
【问题描述】:

我有下面的方法 http 在角度 2 与 {"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);
  }

在我的节点应用程序中,完成了以下事情:

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 cannot load http://rest-0023:3000/api/getAllJobsStagesCount. Response for preflight has invalid HTTP status code 403' 错误,因为我已经设置了所有标头。但我不知道我在哪里遗漏了一些东西。

【问题讨论】:

  • 我遇到了同样的问题。你得到解决方案了吗?

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


【解决方案1】:

您似乎没有拒绝授权方式。我认为标题行应该是这样的:

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

【讨论】:

    猜你喜欢
    • 2016-04-14
    • 2016-02-08
    • 2018-04-05
    • 2018-09-22
    • 2018-05-02
    • 2018-09-30
    • 1970-01-01
    • 2018-01-25
    • 2018-04-11
    相关资源
    最近更新 更多