I have an ASP.Net Core and a Web Api Project in the same solution. I'm calling Get, Post, Put, Delete from my Core Project which is defined in my Api project. Definitely I need to enable CORS.
Bellow is the code that I added in my Web Api web.config file,
<httpProtocol>
<customHeaders>
<!-- Enable Cross Domain AJAX calls -->
<clear />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
And my Ajax code from Core project,
$.ajax({
"url": "myApiUrl",
"type": "Get",
"data": {
//Here I'm passing a value
},
success: function (d) {
}
I set both the projects as startup project. Not an issue. It's calling the Api method and returning the data. But some times it's not returning anything, even the method returns data, I'm getting the error in browser console
that I mentioned in my question title. I checked many answers in SO, including this one, all said that I need to add access control
in my response header
. Any one tell me where I need to add exactly and what I need to add so that it will work as expected.
I checked with Asp.net MVC project too instead Core, same thing happened.
Note: I added CORS in my Web Api Project not in my Web Project.