0
votes

Requests to my API were going swimmingly until I had to POST data. API is expecting JSON so I changed my content-type to application/json, and this causes all CORS implementations to require preflights, and now I'm getting 405. Thing is, request/response looks good. Request headers accept, content-type are in the Access-Control-Request-Headers. Request method POST, POST and PUT allowed. Access-Control-Allow-Origin allowing all origins. What gives?

Remote Address: -
Request URL: -
Request Method: OPTIONS
Status Code: 405 Method Not Allowed
Request Headers
  Accept: */*
  */// for the sake of syntax highlighting
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: en-US,en;q=0.8
  Access-Control-Request-Headers: accept, content-type
  Access-Control-Request-Method: POST
  Cache-Control: no-cache
  Connection: keep-alive
  Host: -
  Origin: http://localhost
  Pragma: no-cache
  Referer: http://localhost/
Response Headers
  Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
  Access-Control-Allow-Origin: *
  Allow: POST,PUT
  Cache-Control: no-cache
  Content-Length: 76
  Content-Type: application/json; charset=utf-8
  Date: Wed, 01 Oct 2014 19:43:56 GMT
  Expires: -1
  Pragma: no-cache
  Server: Microsoft-IIS/8.0
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET

canjs issue

2
Maybe I'm misreading this but doesn't that say your request method is OPTIONS not POSTShriike
Yup, and the response says that only POST and PUT are allowed.dee-see
Yes, so that's what a preflight request is. Since it's a CORS request, it's asking if POST is an accepted method. But you may be on to someting...savinger

2 Answers

1
votes

Related StackOverflow Posts

These posts (1, 2) say to enable CORS on WebApi using Microsoft.AspNet.WebApi.Cors package and offers an implementation of WebApiConfig.cs.

These posts (1, 2, 3) say to remove WebDAV by adding this inside <system.webServer>.

<handlers>
  <remove name="WebDAV"/>
</handlers>
<modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule"/>
</modules>
1
votes
  services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            }));