且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用json将发送请求发送到RESTful WCF

更新时间:2023-11-27 14:51:28

在您的解决方案中添加 Global.ascx 文件并将代码替换为以下

Add a Global.ascx file in youe solution and replace the code with following

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();
    }
}

还有一件事chnage dataType :'text'

one more thing chnage 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");           
    }
});