简体   繁体   English

将后参数传递给WCF服务时出现AngularJS CORS错误

[英]AngularJS CORS error when passing post params to WCF service

Have a WCF webservice running locally ,POST API looks like this ` 有一个在本地运行的WCF Web服务,POST API如下所示:

[WebInvoke(Method = "POST",
     BodyStyle = WebMessageBodyStyle.Wrapped,
     ResponseFormat = WebMessageFormat.Xml)]
        public string AssignLoopToLocations(int LoopID, int[] LocationIDs)
        {
            string strFileName = "AssignLoopToLocations_respone.xml";
            string strResult = GetFileData(strFileName);
            return strResult;
        }

I have added following lines in Web.config of service in system.webserver tag for CORS ` 我在system.webserver标记的CORS服务的Web.config中添加了以下几行

<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
      </customHeaders>
    </httpProtocol>`

` `

My angular side API call looks this ` 我的有角度的侧面API调用看起来如下

factory.assignLocationsToLoop = function ( LoopID,locationList ) {
        var data = {
            LoopID : LoopID, LocationIDs : locationList
        } 
        return $http.post(mediaServicePath + "AssignLoopToLocations", data);

` ` Chrome网络调试器

I am getting this error only when accessing api that has params .When runnin g the same API call in Postman able to get the reponse.Any help would be appreciated. 我只有在访问具有参数的api时才会收到此错误。运行Postman中的相同API调用能够获得响应。任何帮助将不胜感激。

Finally the solution has appeared by making a Global.cs file in WCF service as follows ` 最终,通过在WCF服务中创建Global.cs文件,出现了解决方案,如下所示:

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                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();
            }
        }

` All this with no httpProtocol code mentioned in the above Web.config. `所有这些都没有上述Web.config中提到的httpProtocol代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM