首先引用System.ServiceModel;System.ServiceModel;System.ServiceModel.Activation;命名空间
[ServiceContract] public interface IAPI { ////// 登录 /// /// 用户名 /// 密码 ///[OperationContract(Name = "Login")] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] GeneralResult Login(string UserName, string Password); }
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class APIForApp : IAPI {
上面的是接口和实现代码
global
protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(WebAdmin.API.APIForApp))); }
webconfig
ajax调用
$(function () { $.ajax({ type: "POST", contentType: "application/json", data: '{ "UserName": "111","Password":"111" }', //比如 url: "http://localhost:9800/API/Login", dataType: 'json', error: function (x, e) { console.log(x); }, success: function (response) { console.log(response); } }); });
注意method="动作",动作有POST,GET等,必须大写。