Problem
Whenever I try to sent a POST
request to my WCF service (running locally on IIS), I get:
Access Denied
My WCF config (relevant code only)
I added the below to allow the different methods, and I'm able to get the info from a Request Method: 'GET'
, but not a POST
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="GET,POST,DELETE" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
Ajax call
Here is my ajax call, whenever I try to create a new 'Project'
$.ajax({
url: baseAddress + "projects/create",
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
'Title': 'Test Title',
'Customer': {
'Name': 'Test Name'
}
})
})
Service implementation
Here's the IService.cs
with the UriTemplate
and the Method = 'POST'
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/projects/create")]
int CreateProject(ProjectDTO project);
EXTRA INFO
Whenever I watch the headers through google chrome, I see the Request Method
is OPTIONS
and the response Access-Control-Allow-Methods: GET, POST, DELETE
, but still, I can't make POST
requests (Refer to this).
How do I handle the request method OPTIONS
correctly in a WCF service?
EDIT
The funny thing is that the response says: Access-Control-Allow-Methods: GET, POST, etc.
, which means that since the request asks: Access-Control-Request-Method: POST
, it should be allowed to sent this request, but for some reason I get this Access Denied
EDIT 2
I'm aware that the Request Method: OPTIONS
, but the OPTIONS
asks my service if it's allowed to do a POST
request, where the service answers back with Access-Control-Allow-Methods: GET, POST, etc.
There by saying POST
IS allowed.