Angularjs Post数据使用Asp.net webapi失败

时间:2017-10-25 21:57:25

标签: angularjs ajax asp.net-web-api

我正在使用angularjs我想要发布数据,但它的显示错误低于图片添加。当我调用Get Method时,它成功从服务器检索数据。但发布失败

  

注意:我正在使用ASP.NET WebAPI

$http.post("http://abc/api/plumber", $scope.submitdetails).then(function successCallback(response) {
alert(response.data);

        },
          function errorCallback(response) {

alert(response.data);
            console.log('failed');

          });

}

webconfig

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE,     OPTIONS" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Error Screenshot

1 个答案:

答案 0 :(得分:0)

添加Global.asax

 protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }