用json对象[angular JS]的POST方法无法通过API发送。 api是使用.NET rest服务开发的。 [英] POST method with json object[angular JS] can't able to send through API. The api developed by using .NET rest service.

查看:95
本文介绍了用json对象[angular JS]的POST方法无法通过API发送。 api是使用.NET rest服务开发的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用棱角分明的JS。最初,即使是get方法也不允许这样做。这就是我添加了一些配置设置,如下所示。这只适用于GET,POST方法无效。请帮帮我。





我在< system.server>中的Web.Config中添加了关注代码。元素。





Actaully I was working in angular JS. Initially it is not allowed even the get method also . thats way i added some config settings like below. this is only working for GET, POST Method not working . Please help me.


I added follow code in Web.Config within the <system.server> element.


<httpProtocol>
      <customHeaders>
        <!-- Begin this is for angularJS configuration  -->
      <add name="Access-Control-Allow-Origin" value="*"/>
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        <!-- End this is for angularJS configuration  -->
      </customHeaders>
    </httpProtocol>





我尝试了什么:



我没有尝试过post方法。



What I have tried:

I didn't tried for the post method.

推荐答案

您可以在Global.asax.cs文件中使用以下内容:



You can use following in your Global.asax.cs file:

protected void Application_BeginRequest(object sender, EventArgs e)
        {           
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                //These headers are handling the "pre-flight" OPTIONS call sent by the browser
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }


这篇关于用json对象[angular JS]的POST方法无法通过API发送。 api是使用.NET rest服务开发的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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