C# 从javascript调用asp.net页面方法不起作用

C# 从javascript调用asp.net页面方法不起作用,c#,javascript,asp.net,pagemethods,C#,Javascript,Asp.net,Pagemethods,您好,我正在从javascript调用一个简单的页面方法,下面是我在标记处的代码 function OnCallSumComplete(result, userContext, methodName) { alert(result); } function OnCallSumError(error, userContext, methodName) { if (error !== null) { alert(er

您好,我正在从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" />
警报显示结果,并显示“空”。我检查了firebug,并没有从控制台看到错误

如果我将TestMethod更改为

 [System.Web.Services.WebMethod]
 public static string TestMethod()
 {
    return "Yes this is working";
 }
和PageMethod

 PageMethods.TestMethod( function (response) { alert(response);  } );
它将正确的回答显示为“是,这正在工作”。但是,我需要将参数传递给函数。我错过什么了吗


谢谢您的帮助。

据我所知,您的通话中只需要3个参数(您的参数、onsuccess和onfailure)。你试过使用
测试方法(“测试参数”,OnCallSumComplete,OnCallSumError)

我认为您必须使用[ScriptMethod]而不是[WebMethod],或者在[WebMethod]之外使用[ScriptMethod],才能通过javascript调用使用asmx方法。它之所以可以在不使用参数的情况下工作,是因为请求不必解析任何东西来处理该方法


尝试使用[ScriptMethod](可能还有类定义中的[ScriptService]),看看这是否会产生影响。

我认为主要问题在于用于ScriptManager的程序集

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

要解决您的问题,请在Webconfig中使用-

<pages>
  <controls>
    <add tagPrefix="ajax" 
         namespace="System.Web.UI" 
         assembly="System.Web.Extensions, 
                   Version=1.0.61025.0, 
                   Culture=neutral, 
                   PublicKeyToken=31bf3856ad364e35"/>
  </controls>
</pages>

在.aspx页面中,请使用以下行-

<ajax:ScriptManager ID="ScriptManager1" 
                    EnablePageMethods="true" 
                    runat="server" />


希望这能帮助您解决问题。

问题在于,在Web.config上,您需要启用一个模块(IHttpModule):ScriptModule-4.0。默认情况下,这是启用的,但您可能已将其删除。如果您感到好奇,请在machineweb.config文件中查找它,并查看它是否已从本地Web.config中删除。它的声明应该在system.webServer/modules(对于IIS>=7)和system.web/httpModules(对于Visual Studio的内置web服务器或IIS<7)下。

是的,我这样做了,但仍然收到“null”消息。感谢您的回复。您好,我尝试了[System.Web.Script.Services.ScriptMethod],但在firebug控制台中出现错误“PageMethods未定义”。您是否也添加了ScriptService?如果类不再定义,那是因为定义类接口的javascript文件没有加载。试着看看网络标签,看看有没有什么404’在你身上。我得到了编译错误。“属性'System.Web.Script.Services.ScriptService'在此声明类型上无效。它仅在'class,interface'声明上有效。”我使用WebMethod和Scriptmethod,在firebug上,我可以看到响应为{“d”:“是,这正在工作”}。页面似乎将值分配给了“d”,但我不知道如何从d中检索值。这就是asp.net ajax web服务发送信息的方式,它们将响应封装在一个名为“d”的对象属性中。您可以在此处阅读更多信息:。很高兴它对你有用。
<ajax:ScriptManager ID="ScriptManager1" 
                    EnablePageMethods="true" 
                    runat="server" />