如何处理科夫的飞行前响应 [英] How to deal with preflight response in cors

查看:94
本文介绍了如何处理科夫的飞行前响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Error : Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.







您好我正在使用Web API与json的VS 2010和MVC应用程序。我正在尝试使用我在diff上托管的api。端口通过角$ http。但是我收到了上述错误,我在互联网上查看但无法解决该问题。



#Web API代码:

在Global.asax文件中




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

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




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

            return 1;
            
        }





#Angular代码:



#Angular Code:

$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

推荐答案

http。但是我收到了上述错误,我在互联网上查看但无法解决该问题。



#Web API代码:

在Global.asax文件中

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




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

            return 1;
            
        }





#Angular代码:



#Angular Code:


scope.ClientObj.ClientID = 0 ;
scope.ClientObj.ClientID = 0;


scope.ClientObj。 ClientName =
scope.ClientObj.ClientName =


这篇关于如何处理科夫的飞行前响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆