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

嗨我从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); }  

在cs

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

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

如果我将TestMethod更改为

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

和PageMethod到

  PageMethods.TestMethod( function (response) { alert(response); } ); 

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

感谢帮助。

我认为你必须使用[ScriptMethod]而不是[WebMethod],以便通过javascript调用获得asmx方法。 它可能在没有参数的情况下工作的原因是因为请求不必解析任何东西以便处理该方法。

尝试使用[ScriptMethod](以及类定义中可能的[ScriptService]),看看是否有所作为。

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

  

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

      

在.aspx页面中使用以下行 –

  

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

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

从我记忆中,你只需要3个参数(你的参数,成功和失败)。 你尝试过使用PageMethods.TestMethod(“test parameter”,OnCallSumComplete,OnCallSumError);