Search code examples
jqueryajaxdropdownbox

ajax not calling method in url


This is my first time using ajax so I am not sure where I am going wrong. When a dropdown changes, I call a method in javascript where I want to call a method in the code-behind. This is the markup of the Dropdown:

    <asp:DropDownList ID="ddListSubject" runat="server" ClientIDMode="Static" onchange="SubjectChanged()" CssClass="chosen-single">
        </asp:DropDownList>

This is the javascript function 'SubjectChange()' that is called.

 function SubjectChanged() {
        var strSubject = document.getElementById("ddListSubject").value;
        if (strSubject == "Custom") {
            document.getElementById("txtBoxSubject").value = "";
            document.getElementById("txtBoxSubject").focus();
         }
        else {
            document.getElementById("txtBoxSubject").value = strSubject;
        }
        ShowMaxMsgLength();
        CountChars();
     }

The function 'ShowMaxMsgLength()' contains the ajax code to call a method in the code-behind:

function ShowMaxMsgLength() {                          
        $.ajax({
                type: "POST",
                url: "Default.aspx/GetMaxMsgLength",
                data: "{'id': '1'}",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                success: OnSuccess,
                failure: OnFailure,
                error: function (exception) { alert('Exception:' + exception); }
                });
    };

This is the GetMaxMsgLength() method in the code-behind:

 public static string GetMaxMsgLength(int id)
        {
            string tstrMaxMsgLength = string.Empty;       
            return tstrMaxMsgLength = "32";
        }

I just want it to return '32' for now to see if it is running the method. I know the ShowMaxMsgLength() is called because I put an 'alert' in it.

There is an exception returned: 'Exception: [object Object]'. I don't know what I am not setting that causes this exception.

Thanks.


Solution

  • This was something only a newbie would miss... Added [System.Web.Services.WebMethod] attribute before the method, GetMaxMgsLength.