I have a WCF service (the project is a WCF service library), the service works, I can call this service form Insomnia but when i try to use it in my next.js app, I get this error:
Access to fetch at ‘http://miURL’ 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 know this is a CORS problem but I don’t know hot to enable CORS in my WCF service.
This is my code:
JavaScript:
const test = async () => {
const peticion = await fetch('URL', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
})
const respuesta = await peticion.json()
console.log(respuesta);
}
My Service Code:
public class Service1 : IService1
{
public string leerHuella()
{
Form1 form1 = new Form1();
string value = form1.leerHuellaForm();
return value;
}
}
My iService code:
public interface IService1
{
[OperationContract]
[WebGet]
string leerHuella();
}
My App.config:
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
If someone can help me, that would be great
I have tried some configurations from various tutorials bot none have worked so far.
As a note, I don’t have an asax.cs file and I cant add one, the option is not available in my add item window.
2
Answers
You can try this solution:
Add two new files, Global.asax and Global.asax.cs
Global.asax:
Global.asax.cs:
You can refer to this document:
Enabling CORS in WCF
This issue can also occur if your WCF is deployed on IIS and is not configured.
2.Add the appropriate information