【问题标题】:405 Method Not Allowed Wcf Service and Ionic405 方法不允许 Wcf 服务和离子
【发布时间】:2017-10-10 05:41:28
【问题描述】:

我正在使用 wcf rest api 与 ionic app 进行通信。使用 rest api 时,控制台引发错误“405 Method not allowed”。通过搜索google发现肯定有“CORS错误”。

通过在 Global.asax 文件中添加以下行来重新配置 Wcf 服务

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


        }

然后我得到了

the 'access-control-allow-origin' header contains multiple values

我也尝试在 web.config 文件中添加一个条目,如下所示

<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="GET, POST" />
        <add name="Access-Control-Max-Age" value="1728000" />
      </customHeaders>
    </httpProtocol>

现在我明白了

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://localhost:8100' is therefore not allowed access.

任何帮助

【问题讨论】:

标签: c# wcf cors


【解决方案1】:

我想我现在的问题。请在[ServiceContract] 中使用*。像这样:

[WebInvoke(Method = "*"

这样您就可以让您的方法接收选项请求。

【讨论】:

    猜你喜欢
    • 2016-06-13
    • 2011-01-13
    • 1970-01-01
    • 2014-07-17
    • 2012-10-25
    • 2014-03-30
    • 2012-04-21
    • 1970-01-01
    • 2011-03-04
    相关资源
    最近更新 更多