【问题标题】:Unknown web method in asp.netasp.net 中的未知网络方法
【发布时间】:2015-10-20 20:38:56
【问题描述】:

在下面的代码中,我有一个文本框。我的目标是当我专注于文本框时,它将通过 ajax 调用服务器端代码。但是我得到一个错误未知的 web 方法txtField_GotFocus 参数名称方法名称。请帮我纠正错误。

设计代码:

$(document).ready(function () {
    $("#<%=txtField.ClientID%>").bind("focus", function () {
        $.ajax({
            type: "POST",
            url: "<%=Request.FilePath%>/txtField_GotFocus",
            data: "{}",
            contentType: "application/json",
            dataType: "json",
            success: function (msg) {
                //alert("success message here if you want to debug");
            },
            error: function (xhr) {
                var rawHTML = xhr.responseText;
                var strBegin = "<" + "title>";
                var strEnd = "</" + "title>";
                var index1 = rawHTML.indexOf(strBegin);
                var index2 = rawHTML.indexOf(strEnd);
                if (index2 > index1) {
                    var msg = rawHTML.substr(index1 + strBegin.length, index2 - (index1 + strEnd.length - 1));
                    alert("error: " + msg);
                } else {
                    alert("General error, check connection");
                }
            }
        });
    });
});
<asp:TextBox ID="txtField" runat="server" AutoPostBack="true" OnTextChanged="txtField_TextChanged" ClientIDMode="Static"></asp:TextBox>

field.ascx:

public static void txtField_GotFocus()
{
    string foo = HttpContext.Current.Request["foo"];
    //code...
}

【问题讨论】:

  • 方法上是否需要 WebMethod 属性?
  • 您的 txtField_GotFocus 方法是静态的。不能通过 ajax 调用静态方法...
  • 显示txtField_GotFocus
    参数名方法名
  • @Jason WebMethos 必须是 static stackoverflow.com/questions/18463189/…

标签: asp.net ajax


【解决方案1】:

您缺少[WebMethod] 注释。我也修改了你的代码

[WebMethod]
public static string txtField_GotFocus()
{
    string foo = HttpContext.Current.Request["foo"];//oops i got my super data
    //code...
    return "awesome, it works!";
}

查看Article

重构的 ajax 代码

$(document).ready(function () {
    $("#<%=txtField.ClientID%>").bind("focus", function () {
        $.ajax({
            type: "POST",
            url: "<%=Request.FilePath%>/txtField_GotFocus",
            data: "{foo:'whatever'}",
            success: function (msg) {
                alert(msg);//awesome, it works!
            },
            error: function (xhr) {
            }
        });
    });
});

【讨论】:

  • 你能检查我的 ajax url 以及我在 field.ascx 文件中工作的内容
  • @user3224577 你确定你的 ajax 到达 web 方法而不用 [WebMethod] 装饰它吗?
【解决方案2】:

从服务器返回的数据存储在msg.d 字段中。如果返回单个值,则应使用msg.d 获取它。如果返回 JSON 序列化对象,则必须使用 JSON.parse(msg.d) 解析它

      $(document).ready(function () {
         $("#<%=txtField.ClientID%>").bind("focus", function () {
         $.ajax({
           type: "POST",
           url: "<%=Request.FilePath%>/txtField_GotFocus",
           data: "{foo:'whatever'}",
           success: function (msg) {
                alert(msg.d);//awesome, it works!
           },
            error: function (xhr) {
          }
       });
      });
    });

【讨论】:

    猜你喜欢
    • 2012-04-16
    • 2018-08-22
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多