2014-02-26 30 views
1

我在這裏做錯了什麼?如何編寫用於Ajax調用的ASP.NET WebMethod

我得到這個錯誤:[ArgumentException:未知的Web方法Test2。參數名稱:methodName] System.Web.Script.Services.WebServiceData.GetMethodData(String methodName)+540418 System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender,EventArgs eventArgs)+213 System.Web.SyncEventExecutionStep.System.Web。 HttpApplication.IExecutionStep.Execute()148 System.Web.HttpApplication.ExecuteStep(IExecutionStep步驟,布爾& completedSynchronously)75

<script type="text/javascript" src="/Library/JS/jquery-1.7.1.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     setTimeout(function() { 

      $.ajax({ 
       type: "POST", 
       url: "_debug.aspx/Test2", 
       data: "{ param: 555 }", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 
        console.log(msg); 
       }, 
       error: function (jqXHR, textStatus, errorThrown) { 
        console.log(jqXHR); 
        console.log(textStatus); 
        console.log(errorThrown); 
       } 
      }); 

     }, 1000); 

    }); 

</script> 


Imports System.Web.Services 
Imports System.Web.Script.Services 

Partial Class debug 
Inherits Page 
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     If Not IsPostBack Then 

     End If 
    End Sub 

    <WebMethod(EnableSession:=True)> _ 
    <ScriptMethod(UseHttpGet:=True)> _ 
    Public Function Test(ByVal param As String) As String 
     Return "testing " & HttpContext.Current.Session("UserID") 
    End Function 

    <WebMethod(EnableSession:=True)> _ 
    <ScriptMethod()> _ 
    Public Function Test2(ByVal param As String) As String 
     Return "testing " & HttpContext.Current.Session("UserID") 
    End Function 
End Class 

回答

2

的方法必須共享

<WebMethod(EnableSession:=True)> _ 
<ScriptMethod(UseHttpGet:=True)> _ 
Public Shared Function Test(ByVal param As String) As String 
    Return "testing " & HttpContext.Current.Session("UserID") 
End Function 

<WebMethod(EnableSession:=True)> _ 
<ScriptMethod()> _ 
Public Shared Function Test2(ByVal param As String) As String 
    Return "testing " & HttpContext.Current.Session("UserID") 
End Function 

,並檢查你的腳本管理器允許頁面的方法:

<asp:ScriptManager ID="ScriptManager1" 
EnablePageMethods="true" 
runat="server" /> 
+0

我讀的ScriptManager不需要,我正在尋找一種方法來做到這一點ScriptManager的。這可能嗎? – masteroleary

+0

沒有scriptmanager試過。你的解決方案工作非常感謝 – masteroleary

+0

不客氣! –