1

I have configured Web.Config in web.Server element.

<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://localhost:xxxxx" />
        <add name="Access-Control-Allow-Headers" value="content-type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
      </customHeaders>
</httpProtocol>

When it comes GET Request all data are retrieved.

When I want to POST new element in request, I get error 405 method not allowed. What can be the reason?

Request example: General:

Request URL:http://localhost:xxxxx/api/company
Request Method:OPTIONS
Status Code:405 Method Not Allowed

Response Headers:

Access-Control-Allow-Headers:content-type
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin:http://localhost:yyyyy
Allow:GET,POST
Cache-Control:no-cache
Content-Length:76
Content-Type:application/json; charset=utf-8

Request headers:

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:xxxxx
Origin:http://localhost:yyyyy
Referer:http://localhost:yyyyy/ddddd/zzzzzz
mjwills
  • 23,389
  • 6
  • 40
  • 63
maciejka
  • 818
  • 2
  • 17
  • 42
  • POST your POST method/endpoint :) – Tez Wingfield Oct 27 '17 at 08:28
  • @TezWingfield updated request example. – maciejka Oct 27 '17 at 08:35
  • Thank you, can you add your post method? So we can rule any code issues. – Tez Wingfield Oct 27 '17 at 08:38
  • I use syncfusion javascript that why. – maciejka Oct 27 '17 at 08:39
  • @mjwills Request URL:http://localhost:xxxxx/api/company. CORS is only in web.config like I presented. – maciejka Oct 27 '17 at 08:42
  • What is the URL of the page that this JS is running in? What is the URL of the page it is failing to POST to? – mjwills Oct 27 '17 at 08:43
  • http://localhost:yyyyy/admin/company – maciejka Oct 27 '17 at 08:46
  • I asked two questions. What is the answer to the first question? What is the answer to the second question? Also, please don't use xxxxx and yyyyy - please put the actual values. – mjwills Oct 27 '17 at 08:48
  • Where is your controller code? Are you POSTing to the correct/convention/annotated `action`? – EdSF Oct 29 '17 at 01:57
  • The `Access-Control-Allow-Headers` & `Access-Control-Request-Method` headers shown in the question indicate the `405` you’re getting isn’t for the `POST` from your code but is instead for the CORS preflight `OPTIONS` request the browser (automatically on its own) sends before even trying your `POST`. The solution is to configure handling for `OPTIONS` requests on the backend of the server your frontend code’s sending the request to. See the answer at https://stackoverflow.com/questions/43912655/how-to-enable-cross-origin-requests-in-asp-net-mvc-4-on-post-using-angular-2/43913773#43913773 – sideshowbarker Oct 30 '17 at 09:11

1 Answers1

0

I believe the for the Access-Control-Allow-Origin should "*". Then it might work.