Reputation: 1
I'm building Angular 5 app with asp.net backend. I want to send POST request, I tested it via Postman and it works absolutely fine, however, when I try to do it inside Angular I get 405 error. I user HttpClient with headers: new HttpHeaders({ 'Content-Type': 'application/json' }), same as Postman.
I tried to change web.config
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Origin, Authorization, X-Requested-With, Content-Type, Accept"/>
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
</customHeaders>
</httpProtocol>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
<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>
but it seems it does not work.
Angular POST code (with headers: new HttpHeaders({ 'Content-Type': 'application/json' })
setDopuskItem(itemToSave: DopuskItem){
let endpoint = this._dopuskApiUrl + 'set';
let body = JSON.stringify(itemToSave);
return this.http.put(
endpoint,
body,
httpOptions)
.catch(this.handleError);
}
Upvotes: 0
Views: 359
Reputation: 59
it's because the browsers send preflight request before send main request.
you can add following code to your webapi app: global.asax.cs
protected void Application_BeginRequest()
{
if (Request.HttpMethod == "OPTIONS") {
Response.Flush();
}
}
Upvotes: 1