Question

I have successfully used PageMethods to post a single parameter to a code-behind aspx page, but get the error "Unknown web method" when trying to supply two parameters (or an Object other than a String).

Working code, in my aspx page:

<asp:ScriptManager ID="smAjax" runat="server" EnablePageMethods="true"> </asp:ScriptManager> 

and in an included js file:

function AjaxSuccess(results) {
    alert("AjaxSuccess: " + results);
}
$(document).ready(function () {
    PageMethods.TestAjaxCall("value 1",  AjaxSuccess);
}

and in the code-behind:

<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
<WebMethod> _
Public Shared Function TestAjaxCall(ByVal item1 As String) As String
    Return item1
End Function

However, the following changes result in an exception (System.ArgumentException: Unknown web method TestAjaxCall.): and in an included js file:

function AjaxSuccess(results) {
    alert("AjaxSuccess: " + results);
}
$(document).ready(function () {
    PageMethods.TestAjaxCall("value 1", "value 2", AjaxSuccess);
}

with the concomitant change in the code-behind:

<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
<WebMethod> _
Public Shared Function TestAjaxCall(ByVal item1 As String, ByVal item2 As String) As String
    Return item1 & ": " & item2
End Function
Was it helpful?

Solution

Removing the <ScriptMethod> tag did the trick:

<WebMethod> _
Public Shared Function TestAjaxCall(ByVal item1 As String, ByVal item2 As String) As String
    Return item1 & ": " & item2
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top