从javascript调用asp.net页面方法无法正常工作

win*_*eus 9 javascript c# asp.net pagemethods

嗨我从javascript调用一个简单的页面方法,这是我在标记处的代码

 function OnCallSumComplete(result, userContext, methodName) {             
            alert(result);
 }
 function OnCallSumError(error, userContext, methodName) {
     if (error !== null) {
         alert(error.get_message());
     }
 }
 function test(){
     var contextArray = "";
     PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError,  contextArray);
 }

 <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
Run Code Online (Sandbox Code Playgroud)

在cs

 [System.Web.Services.WebMethod]
 public static string TestMethod(string para)
 {

    return "Yes this is working";
 }
Run Code Online (Sandbox Code Playgroud)

警报显示结果,并显示"null".我检查firebug,我没有看到控制台的错误.

如果我将TestMethod更改为

 [System.Web.Services.WebMethod]
 public static string TestMethod()
 {
    return "Yes this is working";
 }
Run Code Online (Sandbox Code Playgroud)

和PageMethod到

 PageMethods.TestMethod( function (response) { alert(response);  } );
Run Code Online (Sandbox Code Playgroud)

它显示正确的响应为"是这是有效的".但是,我需要将参数传递给函数.我什么都想念?

感谢帮助.

小智 2

我认为您必须使用 [ScriptMethod] 代替 [WebMethod] 或除了 [WebMethod] 之外,才能通过 javascript 调用使用 asmx 方法。它可以在不带参数的情况下工作的原因是因为请求不必解析任何内容来处理该方法。

使用 [ScriptMethod](可能还有类定义中的 [ScriptService])尝试一下,看看是否有区别。