C# 方法不允许(http错误405)、wcf、rest服务、post方法
我试图从html、ajax脚本调用在wcf中实现的post方法。 我可以从邮递员那里调用。我试过谷歌,回答了前面的问题,但没有一个对我有帮助 wcf中的My web.config:C# 方法不允许(http错误405)、wcf、rest服务、post方法,c#,ajax,rest,wcf,post,C#,Ajax,Rest,Wcf,Post,我试图从html、ajax脚本调用在wcf中实现的post方法。 我可以从邮递员那里调用。我试过谷歌,回答了前面的问题,但没有一个对我有帮助 wcf中的My web.config: <standardEndpoints> <webScriptEndpoint> <standardEndpoint name="" crossDomainScriptAccessEnabled="true" /> </webScri
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
</webScriptEndpoint>
</standardEndpoints>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,OPTIONS" />
</customHeaders>
</httpProtocol>
以下是邮递员的照片
下面是html控制台的图像
这是CORS问题,您可以通过在wcf服务上创建global.asax文件来启用CORS
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", "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();
}
}
但是我的wcf服务没有global.asax文件。很抱歉这个愚蠢的问题,因为我是c#新手。添加新文件(global.asax)还好吗?是的,应该可以。如果您有任何问题,请检查此项
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", "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();
}
}