飞行前的响应具有无效的HTTP状态代码404-Angular 4和Web API 2 [英] Response for preflight has invalid HTTP status code 404 - Angular 4 and Web API 2

查看:53
本文介绍了飞行前的响应具有无效的HTTP状态代码404-Angular 4和Web API 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在该服务中,我正在调用Web API(2)(与前端angular 4应用程序托管在不同的服务器上)以获取一些信息.

In the service I am calling Web API(2)(hosted on different server than the front end angular 4 application) action method in order to retrieve some info.

该操作方法需要使用登录到应用程序后收到的访问令牌进行的授权.现在,该代码可以在Microsoft Edge中正常工作,但是却给了我chrome的预检错误,我认为这是CORS问题.不确定如何解决此问题.有人可以帮我吗?

The action method needs authorization which I am doing using access token that I receive after logging into the application. Now, the code works fine in Microsoft edge, however gives me preflight error in chrome which I guess is CORS issue. Not sure how to resolve this. Can someone help me with this?

这是我的代码;

 getCompanyInfo(id: string) {
  let headers = new Headers({
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      'Authorization': 'Bearer ' + this.commonService.accessToken
    });
  let options = new RequestOptions({ headers: headers });
  let getRequestUrl = this.commonService.baseUrl + 'api/getCompanyInfo';

    return this.http.get(getRequestUrl + '/' + id, options)
      .map((res: any) => <CompanyInfoModel>res.json())
      .do(response => console.log(response))
      .catch(this.commonService.handleError);
  }

请注意,这很好;

  1. 如果我不使用对Web API的授权访问,请从标头中删除令牌.

  1. If I do not use authorized access to Web API and remove the token from the headers.

在Microsoft Edge中,标头中是否包含访问令牌.

In Microsoft edge with or without Access Token in headers.

推荐答案

除以下内容外,对我没有任何帮助;我必须导入Microsoft.Owin.Cors并将此代码行添加到Web API Startup.Auth.cs的ConfigureAuth方法顶部的位置.

Nothing worked for me except the below; Where I had to import Microsoft.Owin.Cors and add this line of code at the top of ConfigureAuth method in Startup.Auth.cs of Web API.

 app.UseCors(CorsOptions.AllowAll);

请注意:-从Web.configWebApiConfig.cs中删除所有用于启用CORS的设置.否则,它将抱怨重复的实现.

Please note:- remove all the settings for enabling CORS from Web.config and WebApiConfig.cs. Otherwise it will complain about duplicate implementation.

快乐编码:-)

这篇关于飞行前的响应具有无效的HTTP状态代码404-Angular 4和Web API 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆