我正在为我的API设置一个WebAPI端点,但是无法将我的AngularJS调用到我的PostRegister方法工作。
原文链接:https://www.f2er.com/angularjs/144162.htmlwebAPI和Angular位于不同的站点上。
在Startup.cs我有:
public void Configuration(IAppBuilder app) { ConfigureOAuth(app); var config = new HttpConfiguration(); WebApiConfig.Register(config); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); app.UseWebApi(config); } private void ConfigureOAuth(IAppBuilder app) { var oAuthServerOptions = new OAuthAuthorizationServerOptions { AllowInsecureHttp = true,TokenEndpointPath = new PathString(Paths.TokenPath),AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),Provider = Kernel.Get<IOAuthAuthorizationServerProvider>() }; app.USEOAuthAuthorizationServer(oAuthServerOptions); OAuthBearerOptions = new OAuthBearerAuthenticationOptions(); app.USEOAuthBearerAuthentication(OAuthBearerOptions); }
然后在控制器上,我的方法设置为允许匿名:
[AllowAnonymous] public bool PostRegister(UserSignupForm form) { ... }
我也更新了web.config:
<system.webServer> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <remove name="OPTIONSVerbHandler" /> <remove name="TRACEVerbHandler" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> </system.webServer>
在角度方面,我做了一个简单的$ http.post调用:
saveRegistration: function (registration) { return $http.post(baseUrl + '/api/signup/register',registration).then(function (response) { return response; }); }
当使用Postman进行调用时,我得到了一个成功的响应,但是当从Angular进行相同的调用时,我得到了405错误。
BitOfTech示例站点正在发送相同的请求标头,但能够获取Access-Control-Allow-XXX标头
我已经更新了我的代码以跟随Token Based Authentication using ASP.NET Web API 2,Owin,and Identity