hi i am working on MVC ,Angularjs ,Webapi( i am using UnityConfig) Project where in my solution webapi project and MVC project are seprate project so becasuse of that reason i am facing problem to call my api so to solve this issue i have added Cross Origin Resource Sharing (CORS) using this policy i am Abel to call GET method but when i am trying to call POST method it gives me this error (Response for preflight has invalid HTTP status code 400),i am using angularjs services to call my WebAPI but i am Unable to call WebAPI, i am sharing my code please review and please help me to sort out this issue ASAP.
What I have tried:
$scope.Save = function Save(GenderBO) {
var promise = GenderServ.post(GenderBO);
promise.then(function (Response) {
$scope.Gender = Response.data;
}, function (error) {
$scope.Message = "error" + error.status;
});
};
App.service('GenderServ', function ($http) {
this.get = function () {
var Response = $http.get("http://localhost:57185/api/GenderAPI/");
return Response;
};
this.post =(function (GenderBO) {
$.ajax({
type: 'POST',
url: 'http://localhost:57185/api/GenderAPI/',
data: JSON.stringify(GenderBO),
contentType: 'application/json',
dataType: 'json',
success: function (GenderBO) { console.log(GenderBO) }
});
});
this.post = function (GenderBO) {
var Response = $http({
method: "post",
url: "http://localhost:57185/api/GenderAPI/Insert",
data: GenderBO,
contentType: "application/json",
dataType: 'json',
});
return Response;
}
[EnableCors(origins: "http://localhost:57185", headers: "*", methods: "*")]
public class GenderAPIController : ApiController
{
IRepository<genderbo,> _repository;
public GenderAPIController(IRepository<genderbo,> repo)
{
_repository = repo;
}
[ResponseType(typeof(IEnumerable<genderbo>))]
public IHttpActionResult Get()
{
var Response = _repository.Get();
return Ok(Response);
}
[HttpPost]
[ResponseType(typeof(GenderBO))]
public IHttpActionResult Insert(GenderBO id)
{
var Response = _repository.Insert(id);
return Ok(Response);
}
config.EnableCors(cors);