我正在尝试调用post方法,该方法在wcf中实现,来自html,ajax脚本。 我可以从邮递员那里调用。我试过谷歌,stackoverflow以前的问题,但没有一个帮助我。
来自wcf的我的web.config:
<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中的ajax调用
$.ajax({
type: "POST",
url: "http://localhost:8078/SportsClubDefault.svc/Validate",
data: st,
contentType: "application/json; charset=utf-8",
processData:true,
dataType: "json",
success: function (data) {
// Play with response returned in JSON format
alert('Login success');
},
error: function (xhr) {
alert('Login failed');
}
});
答案 0 :(得分:2)
这是一个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();
}
}