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", "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(); } }
$(document).ready(function () { $("button").click(function () { alert("clicked"); var data = $("#txt").val(); var postdata = {}; var data_obj = {"BoolValue" : "true" , "StringValue": data} postdata["value"] = data_obj; var url = "https://tmdev01.tm00.com/testwcf/service1.svc/TestMethod"; $.ajax({ type: "POST", url: url, contentType: "application/json; charset=utf-8", data: JSON.stringify(postdata), dataType: "json", success: function(data) {console.log(data);}, error: function(a,b,c) {console.log(a);} }); }); });
https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api