0

In the below code I have a textbox .My aim is when I focus on textbox it will call the server side code through ajax.But I got a error unknown web method txtField_GotFocus parameter name method name.Please help me to rectify the error.

design code:

$(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...
}
Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
user3224577
  • 37
  • 1
  • 1
  • 9

2 Answers2

0

You are missing [WebMethod] annotation. Also i have modified your code

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

Check this Article

Your refactored ajax code

$(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) {
            }
        });
    });
});
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
0

Data returned form Server is stored in msg.d field. If you return a single value, you should get it using msg.d. If you return a JSON serialized object you have to parse it using 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) {
          }
       });
      });
    });
anmarti
  • 5,045
  • 10
  • 55
  • 96