预检响应具有无效的 HTTP 状态代码 404 - Angular 4 和 Web API 2
Response for preflight has invalid HTTP status code 404 - Angular 4 and Web API 2
在服务中,我正在调用 Web API(2)(托管在与前端不同的服务器上 angular 4 应用程序)操作方法以检索一些信息。
操作方法需要授权,我正在使用登录应用程序后收到的访问令牌进行授权。现在,该代码在 Microsoft edge 中运行良好,但是在 chrome 中给我预检错误,我猜这是 CORS 问题。不知道如何解决这个问题。有人可以帮我解决这个问题吗?
这是我的代码;
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);
}
请注意,这工作正常;
如果我不使用授权访问 Web API 并从 headers 中删除令牌。
在 Microsoft edge 中,在 headers 中有或没有访问令牌。
除以下内容外,对我没有任何帮助;我必须导入 Microsoft.Owin.Cors
并在 Web API.
的 Startup.Auth.cs
中的 ConfigureAuth 方法顶部添加这行代码
app.UseCors(CorsOptions.AllowAll);
请注意:- 从 Web.config
和 WebApiConfig.cs
中删除所有启用 CORS 的设置。否则会抱怨重复实现。
快乐编码:-)
在服务中,我正在调用 Web API(2)(托管在与前端不同的服务器上 angular 4 应用程序)操作方法以检索一些信息。
操作方法需要授权,我正在使用登录应用程序后收到的访问令牌进行授权。现在,该代码在 Microsoft edge 中运行良好,但是在 chrome 中给我预检错误,我猜这是 CORS 问题。不知道如何解决这个问题。有人可以帮我解决这个问题吗?
这是我的代码;
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);
}
请注意,这工作正常;
如果我不使用授权访问 Web API 并从 headers 中删除令牌。
在 Microsoft edge 中,在 headers 中有或没有访问令牌。
除以下内容外,对我没有任何帮助;我必须导入 Microsoft.Owin.Cors
并在 Web API.
Startup.Auth.cs
中的 ConfigureAuth 方法顶部添加这行代码
app.UseCors(CorsOptions.AllowAll);
请注意:- 从 Web.config
和 WebApiConfig.cs
中删除所有启用 CORS 的设置。否则会抱怨重复实现。
快乐编码:-)