ajax不会在url中调用方法

ajax不会在url中调用方法

问题描述:

这是我第一次使用ajax,所以我不确定我哪里出错了。 当下拉列表发生变化时,我在JavaScript中调用一个方法,在该方法中,我想调用代码隐藏方法。 这是下拉的标记:ajax不会在url中调用方法

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

这是JavaScript函数“SubjectChange()”被调用。

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(); 
    } 

函数 'ShowMaxMsgLength()' 包含Ajax代码来调用代码隐藏的方法:

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); } 
       }); 
    }; 

这在代码隐藏GetMaxMsgLength()方法:

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

我只是想让它现在返回'32',看它是否正在运行该方法。 我知道ShowMaxMsgLength()被调用,因为我在其中放置了一个'alert'。

返回异常:'异常:[object Object]'。 我不知道我没有设置,导致此异常。

谢谢。

这只是一个新手会错过的东西... 已添加 [System.Web.Services.WebMethod]属性之前的方法,GetMaxMgsLength。