Angular2和Node:预检的响应具有无效的HTTP状态代码403

时间:2016-08-19 12:55:02

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

对于角度为2的http我有以下方法,标题设置为{"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'错误,因为我已经设置了所有标头。但我并不确切地知道我错过的地方。

1 个答案:

答案 0 :(得分:0)

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

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