I got many solution no one helped for me. i have followed all the solutions set all required headers. still its showing the below error.
Fetch API cannot load http://localhost:25424/api/Employee/DeleteEmployee/1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7777' is therefore not allowed access. The response had HTTP status code 404. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
my fetch request :
fetch('http://localhost:25424/api/Employee/DeleteEmployee/' +1, {
method: "DELETE",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin' : '*' ,
'Access-Control-Allow-Headers' : 'Origin, X-Requested-With, Content-Type, Accept',
'Access-Control-Allow-Methods' : 'DELETE',
'mode' : 'cors'
},
})
.then(function(resp){
})
I tried set mode : no-cors also.
My web api code :
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class EmployeeController : ApiController
{
[HttpDelete]
public IHttpActionResult DeleteEmployee(int id)
{
using (var ctx = new Employee())
{
var existingemp = ctx.tempemp.Where(s => s.Id == id).FirstOrDefault();
ctx.Entry(existingemp).State = System.Data.Entity.EntityState.Deleted;
ctx.SaveChanges();
}
return Ok();
}
}
I have set CORS in WebApiConfig.cs also
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
Thanks in advance.
http://localhost:25424/api/Employee/DeleteEmployee/
via the browser ... maybe firefox , and once you get a response, Open dev console network panel, doedit and resend
AddOrigin:http://localhost
and post the new response headers here? Also, you Should Not add anyaccess-control
headers in the fetch request. These headers are supposed to be response headers – Schrodinger's cat