405方法不允许Wcf服务和离子 [英] 405 Method Not Allowed Wcf Service and Ionic

查看:94
本文介绍了405方法不允许Wcf服务和离子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wcf rest api与ionic app通信。在使用rest api时,控制台引发了错误 405方法不允许。通过搜索Google,发现必须存在 CORS错误。

I am using wcf rest api to communicate with ionic app . While consuming rest api the console raised an error "405 Method not allowed". By searching google found there must be a "CORS error" .

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

Re Configured Wcf service by added the following lines inside Global.asax file

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文件中添加如下条目

I have also tried by adding an entry inside web.config file as below

<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.

任何帮助

推荐答案

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

i guess i now the problem. please use * in [ServiceContract]. like this:

[WebInvoke(Method = "*"

这样,您允许您的方法接收选项请求。

this way you allow your method receive options request.

这篇关于405方法不允许Wcf服务和离子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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