Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
Error : Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.



Hi I am using Web API with VS 2010 and MVC application with json. I am trying to consume my api hosted on diff. port via angular $http. But I am receiving the above error, I looked on internet but could not get what to do resolve it.

# Web API code:
In Global.asax file
C#
protected void Application_BeginRequest()
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:52298");

            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Method", "OPTIONS, GET, POST, PUT, DELETE");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Header", "accept, content-type"); //
                HttpContext.Current.Response.End();
            }
        }

C#
[HttpPost]
        public int SaveClient(string ClientObj)
        {
            if (ClientObj == null)
                return -2;

            return 1;
            
        }


#Angular Code:
JavaScript
$scope.ClientObj.ClientID = 0;
        $scope.ClientObj.ClientName = $scope.clientName;
        $scope.ClientObj.Description = '';
        $scope.ClientObj.ClientType = $scope.clientType;
        $scope.ClientObj.ClientAddress = $scope.clientAddress;
        $scope.ClientObj.Email = $scope.clientEmail;
        $scope.ClientObj.Phone = $scope.clientPhone;
        $scope.ClientObj.IsActive = true;
        $scope.ClientObj.CreatedBy = 0;
        console.log($scope.ClientObj);
        var a = JSON.stringify($scope.ClientObj);
        console.log(a);

        $http({
            url: 'http://localhost:54212/api/Client/SaveClient/'
            , method: 'POST'
            , data: 'hello' //$scope.Clientobj
            , header: {'content-type':'application/text'}
        }).success(function (response) {
            console.log('success', response);
            alert(response);
        }).error(function (error) {
            console.log('error');
            alert(error);
        });


Thanks
Posted
Updated 17-Dec-15 0:04am
v2

1 solution

During the preflight request, you should see the following two headers: Access-Control-Request-Method and Access-Control-Request-Headers. These request headers are asking the server for permissions to make the actual request. Your preflight response needs to acknowledge these headers in order for the actual request to work.

For example, suppose the browser makes a request with the following headers:

Origin: http://yourdomain.com
XML
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-Custom-Header

Your server should then respond with the following headers:

Access-Control-Allow-Origin: http://yourdomain.com
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: X-Custom-Header
Pay special attention to the Access-Control-Allow-Headers response header. The value of this header should be the same headers in the Access-Control-Request-Headers request header, and it can not be '*'.

Once you send this response to the preflight request, the browser will make the actual request. You can learn more about CORS here: http://www.html5rocks.com/en/tutorials/cors/

Referance : http://stackoverflow.com/questions/8685678/cors-how-do-preflight-an-httprequest[^]
 
Share this answer
 
v2

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