I know the question has been asked many time but i still didn't find the solution.
Am having a cross domain error when i tried to communicate with my API from the client. I don't know if
there's something am missing here.
XMLHttpRequest
cannot load http://localhost:28184/Customer/AddCustomer
.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin
http://localhost:28189
is therefore not allowed access. The response had HTTP status code 404.
Ajax call:
function AddCompany()
{
var companyModel = {
----------
}
$.support.cors = true;
$.ajax({
url: BaseUrl + "Customer/AddCustomer",
type: "POST",
withCredentials: false,
dataType: "Jason",
cach: false,
async: true,
crossDomain: true,
data: companyModel,
success: function(data){
alert("Company has been successfully registered");
}
});
}
API controller:
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using Fleet_API.Models;
namespace Fleet_API.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
[RoutePrefix("Api/Customer")]
public class CustomerController : ApiController
{
[Route("AddCustomer")]
[HttpPost]
public async Task<bool>AddCompany(CustomerModel model)
{
------------------------
}
}
}
Hosts file from windows
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost