Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
//Cal service For Insert Gender Details//
//this is my controller//
    $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;
        });
    };


//this is my angular Service//
App.service('GenderServ', function ($http) {


// My GET Method is working in this case//
    this.get = function () {      
        var Response = $http.get("http://localhost:57185/api/GenderAPI/");
        return Response;
    };
    
//i am try both way using ajax call or using angular call but no any Responce//


    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) }
        });
    });

//Or this way 
    this.post = function (GenderBO) {
        var Response = $http({
            method: "post",
            url: "http://localhost:57185/api/GenderAPI/Insert",
            data: GenderBO,
            contentType: "application/json",
           // headers: { 'Content-Type': 'application/json' },
            dataType: 'json',
        });
        return Response;
    }


//My WEBAPI code(C#)//

[EnableCors(origins: "http://localhost:57185", headers: "*", methods: "*")]
    public class GenderAPIController : ApiController
    {
        IRepository<genderbo,> _repository;
        public GenderAPIController(IRepository<genderbo,> repo)
        {
            _repository = repo;
        }

        // GET api/genderapi
       
       [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);
       }

///added this code in my WebApiConfig//
 config.EnableCors(cors);

//i have done this change in my WEBAPI projects Web.config(Access-Control-Allow-Origin)//</genderbo>
Posted
Updated 24-May-16 21:01pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900