I have a SPA application and it consumes a REST API done in ASP.NET WebAPI.
When trying to perform an insert in the API, the following error is returned:
Response for preflight has invalid HTTP status code 405
In web.config
of WebAPI I configured like this:
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
</customHeaders>
I do not know why this happens.
Non-angled:
$scope.inserirCarro = function () {
var carro = {
CarroNome: $scope.nome,
MinCod: $scope.selectMin
};
var carroData = crudService.IncluirCarro(carro);
carroData.then(function (data) {
getCarros();
if (data.status == 200) {
toastr["success"]("Registro cadastrado com sucesso!", "Sistema");
}
}, function () {
toastr["error"]("Erro ao incluir!", "Sistema");
});
}
Non-angular service:
this.IncluirCarro = function (carro) {
var response = $http({
method: "post",
url: urlAPI + "/api/v1/carros/postcarro",
data: JSON.stringify(carro),
dataType: "json"
});
return response;
}