【问题标题】:Calling asp.net page method from javascript not working从javascript调用asp.net页面方法不起作用
【发布时间】:2012-05-18 18:41:33
【问题描述】:

您好,我正在从 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”。我检查了萤火虫,但没有从控制台看到错误。

如果我将 TestMethod 更改为

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

和PageMethod来

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

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

感谢您的帮助。

【问题讨论】:

    标签: c# javascript asp.net pagemethods


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      尝试使用 [ScriptMethod](可能还可以在您的类定义中使用 [ScriptService]),看看是否会有所不同。

      【讨论】:

      • 您好,我使用 [System.Web.Script.Services.ScriptMethod] 进行了尝试,但在 firebug 控制台中出现错误“PageMethods is not defined”。
      • 你也添加了 ScriptService 吗?如果不再定义类,那是因为定义类接口的 javascript 文件没有加载。尝试查看网络选项卡,看看您是否有任何 404 问题。
      • 我得到编译错误。 "属性 'System.Web.Script.Services.ScriptService' 在此声明类型上无效。它仅在 'class, interface' 声明上有效。"
      • 我使用 webmethond 和 Scriptmethod,在 firebug 上,我可以看到响应是 {"d":"Yes this is working"} 。页面似乎将值分配给“d”,但我不知道如何从 d 中检索值。
      • 这是 asp.net ajax web 服务发送信息的方式,它们将响应包装在一个名为“d”的对象属性中。您可以在此处阅读更多信息:encosia.com/never-worry-about-asp-net-ajaxs-d-again。很高兴它对你有用。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        据我所知,您的通话中只需要 3 个参数(您的参数、onsuccess 和 onfailure)。你试过用吗 PageMethods.TestMethod("测试参数", OnCallSumComplete, OnCallSumError);

        【讨论】:

        • 是的,我做到了,但仍然收到“null”消息。感谢您的回复。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多