Add a Global.ascx
文件中的解决方案并将代码替换为以下内容
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();
}
}
还有一件事改变dataType:'text'
$.ajax({
type: "POST",
url: "http://localhost:4638/Edulink.svc/SaveUserData",
dataType: "text",
contentType: "application/json",
data:'{"EmailID":"praveen", "LevelID": 1}',
success:function(data, status) {
console.log(data); //gives 1
},
error:function(request, status, error) {
alert("o0ops");
}
});