ajax 呼叫 WebApi 方法
阿新 • • 發佈:2020-11-16
WebApi跨域設定:
public static void Register(HttpConfiguration config) { //跨域設定 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); // Web API 配置和服務 // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name:"DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); }
ajax 呼叫方法:
$("#tijiao").on("click", function () { var balance = { "SubCom_Code": $("#subcompany_in").val(),"BooK_Id": $("#bookid_in").val(), "Card_Id": $("#cardid_in").val(), "Card_Address": $("#cardaddress_in").val(), "Book_UserId": $("#bookuser_in").val(), "Contents": $("#content_in").val() }; $.ajax({ url: URI.BalanceRegister.InsertBalance, dataType:'json', data: JSON.stringify(balance), type: 'POST', 'Content-Type': 'application/json', success: function (response) { if (response.Code == 200) { layer.alert('新增成功'); } else { layer.alert('新增失敗'); } } }); })