2015-04-02 101 views
0

這是我第一次使用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]'。 我不知道我沒有設置,導致此異常。

謝謝。

回答

0

這只是一個新手會錯過的東西... 已添加 [System.Web.Services.WebMethod]屬性之前的方法,GetMaxMgsLength。