WebMethod没有被调用

我通过jquery.ajax将包含字符串的javascript变量传递给服务器。 虽然调用了“成功”条件,但从不调用服务器端WebMethod。 客户:

$.ajax({ type: "post", url: "Playground.aspx/childBind", data: {sendData: ID}, //contentType: "application/json; charset=utf-8", dataType: "text", success: function (result) { alert("successful!" + result.d); } }) 

服务器:

 [WebMethod] public static string childBind(string sendData) { return String.Format("Hello"); } 

尝试以下针对Ajax请求的修复:

  $.ajax({ type: "post", url: "Playground.aspx/childBind", data: "{sendData: '" + ID + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { alert("successful!" + result.d); } }) 

注意将dataTypedata值更改为字符串。

我遇到了同样的问题。 谷歌搜索后,我找到了解决方案,它对我有用。 导航到RouteConfig.cs并注释掉下面的行:

 public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { var settings = new FriendlyUrlSettings(); //settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings); } } 

我想添加一个注释:您的“ID”(或其他字段)字符串包含数字错误,如“=”。 解决这个问题:

 var DTO = {'sendData': ID}; $.ajax({ "type": "POST", "dataType": 'json', "contentType": "application/json; charset=utf-8", "url": sSource, "data": JSON.stringify(DTO), "success": function (msg) { //do something } }); 

试试这样:JQuery:

  var dataString = JSON.stringify({ contractName: contractName, contractNumber: contractNumber }); $.ajax({ type: "POST", url: "CreateQuote.aspx/GetCallHistory", data: dataString, contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { alert(result); OpenLightBox('divDelete'); } }); 

ASPX.CS:

  [System.Web.Services.WebMethod] public static string GetCallHistory(string contractName, string contractNumber) { return "Nalan"; }