【发布时间】: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.
任何帮助
【问题讨论】: