Angular 启用CORS时不允许使用WebAPI方法
我正在使用Angular 5组件的WebAPI 2.0 Post方法。WebAPI启用了CORS(显示在Chrome的响应选项卡中),但点击它时,显示为MethodNotAllowed。不知道这里出了什么问题。身份验证正在使用承载令牌。邮递员的袭击是恰当的。但在通过时无法命中apiAngular 启用CORS时不允许使用WebAPI方法,angular,post,asp.net-web-api2,Angular,Post,Asp.net Web Api2,我正在使用Angular 5组件的WebAPI 2.0 Post方法。WebAPI启用了CORS(显示在Chrome的响应选项卡中),但点击它时,显示为MethodNotAllowed。不知道这里出了什么问题。身份验证正在使用承载令牌。邮递员的袭击是恰当的。但在通过时无法命中api private PopulateHeaders() { this.userIdAuthToken = localStorage.getItem(this.authToken); let head
private PopulateHeaders() {
this.userIdAuthToken = localStorage.getItem(this.authToken);
let headers = new HttpHeaders({
'Content-Type': 'application/json'
});
// Once security is implemented, add the token here
if (this.userIdAuthToken)
headers.append('Authorization', 'Bearer ' + this.userIdAuthToken);
this.httpOptions = {
headers: headers
};
}
postProject(name: string, details: string): Observable<HttpEvent<Project>> {
var data = { name: name, details: details };
this.PopulateHeaders();
let saveRequest = this._http.post<Project>(
this.apiUrl + '/project' + '/post',
JSON.stringify(data),
this.httpOptions);
return saveRequest;
}
(只需提及)
我在这里使用OWIN身份验证,我的启动文件包含以下内容:-
public void Configuration(IAppBuilder app)
{
// CORS has to be set first in order for it to be enabled for OWIN
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
this.ConfigureOAuthTokenGeneration(app);
this.ConfigureOAuthTokenConsumption(app);
this.ConfigureWebApi();
app.UseWebApi(HttpConfiguration);
}
如果我首先调用“UseCors”,web api将报告cors失败,如果我将其添加到oauth之后,它将报告oauth上的cors错误。。。不知道在哪里设置它 在Global.asax.cs文件中添加以下代码
protected void Application_BeginRequest()
{
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.Flush();
}
}
您必须在服务器端启用
OPTIONS
http方法。将其添加到Web.config
中的
字段:
<!-- Enable OPTIONS http request-->
<remove name="OPTIONSVerbHandler" />
<add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="ProtocolSupportModule" resourceType="Unspecified" requireAccess="Script" />
选项
在每次post和put请求之前发送,默认情况下禁用。最终,这篇文章帮助了我
无需更新以下内容:-
public void Configuration(IAppBuilder app)
{
// CORS has to be set first in order for it to be enabled for OWIN
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
this.ConfigureOAuthTokenGeneration(app);
this.ConfigureOAuthTokenConsumption(app);
this.ConfigureWebApi();
app.UseWebApi(HttpConfiguration);
}
- app.UseCors(因为CustomOAuthProvider已经为所有人设置了Cors)
- WebAPI需要使用Cors,所以最好的注入方式是通过WebAPI.Cors Nuget和EnableCors方法
- 选项或标题的web.config没有更改(访问控制-*)
var cors = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
启动
谢谢大家的支持和帮助试试Fiddler,它会给你准确的错误。我认为,这些参数与API方法的期望值不一样。您能同时显示WebAPI代码吗?您确定这是一种后API方法吗?这可能是你的。检查您的API一次。@Dherajkumar添加了webapi代码。删除JSON.stringify以获得Payload没有任何区别。导致“网络::错误连接\u重置”
public void Configuration(IAppBuilder app)
{
// **No longer required**
// app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
this.ConfigureOAuthTokenGeneration(app);
this.ConfigureOAuthTokenConsumption(app);
this.ConfigureWebApi();
app.UseWebApi(HttpConfiguration);
// No further configuration is now allowed.
HttpConfiguration.EnsureInitialized();
}