I have a .netframework(4.5.2) Console Application which have RESTful WCF Service.
I have a problem with using rest service from Javascript client.
When I use Postman to consume rest service, there is no problem.
When I use Javascript fetch method, there is a CORS error
from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I tried below solution from google;
1- Adding Web.config customHeaders.
however, there is no web.config, i add the below code App.config
<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="POST,GET,OPTIONS" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
2- Global.asax
Global.asax solution for a web project
Because of reason mentioned before, there is no Global.asax. I cant try this.
3- WCF Builder
I allowed this CrossDomain control when build wcf service. This is not workin too.
var binding = new WebHttpBinding(WebHttpSecurityMode.None);
binding.CrossDomainScriptAccessEnabled = true;
Thanks for the advice.
EDIT
I also create a test application on github. You can see there Postman request reach service method, but javascript request does not. It gives below error.
https://github.com/mmustafau/StackoverServiceTestnet
...has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
my javascript request is below.
let receiptJson = {
"Email": "[email protected]",
"Name": "asdasd",
"Password": "asdasd"
}
const requestOptions = {
method: 'POST',
headers:{ 'Content-Type': 'application/json',
},
body: JSON.stringify (receiptJson)
};
return fetch("http://localhost:8070/ServiceModelSamples/service/user", requestOptions)
.then(handleResponse)
.then(receiptJson => {
return receiptJson;
});
There are two ways to solve cross domain problems in WCF. The first is to add a global configuration file to the WCF project.After the project is deployed to IIS, IIS will read the added global configuration file to solve cross domain problems, just like a web project.
Modify global profile to solve cross domain problems.
The second way is to make WCF support jsonp.We can enable JSONP in the configuration file.
UPDATE
You can implement idispatchmessageinspector to add response headers before the service responds.
For more information about IDispatchMessageInspector,Please refer to the following link:
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.idispatchmessageinspector?view=netframework-4.8
CODE
Add behavior to service
This is my project directory
App.config
dao.cs
IService1.cs
Program.cs
Service1.cs
soap.cs
Ajax