繁体   English   中英

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

[英]Calling asp.net page method from javascript not working

嗨我从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" />

在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);  } );

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

感谢帮助。

我认为主要问题在于您用于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" />

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM