为什么这个页面方法失败了?

发布于 2024-08-08 11:22:11 字数 958 浏览 13 评论 0原文

我的脚本管理器的属性 enablepagemethods 设置为 true,但是,由于某种原因,这提醒我失败了。

 public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod()]
    public static string test()
    {
        return "q343242342342";
    }
}

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
  function callMethod() {
      PageMethods.test(onSuccess, onFailure);
  }

  function onSuccess(result) {
      alert(result.d);
  }

  function onFailure(error) {
      alert('fail');
  } 
         </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server"  EnablePageMethods="True">
 </asp:ScriptManager>
 <div>
    <asp:Button ID="Button1" OnClientClick="callMethod()" runat="server" Text="Button"/>

I have my script manager with attribute enablepagemethods set to true, however, for some reason this is alert me I've failed.

 public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod()]
    public static string test()
    {
        return "q343242342342";
    }
}

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
  function callMethod() {
      PageMethods.test(onSuccess, onFailure);
  }

  function onSuccess(result) {
      alert(result.d);
  }

  function onFailure(error) {
      alert('fail');
  } 
         </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server"  EnablePageMethods="True">
 </asp:ScriptManager>
 <div>
    <asp:Button ID="Button1" OnClientClick="callMethod()" runat="server" Text="Button"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-08-15 11:22:11

这段代码对我有用 - 无法准确判断您的问题出在哪里,但您在 runat=server 块中没有代码(假设您只是从后面的代码复制)。但我认为这个确切的代码应该有效。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<html>
<head runat="server">
<title>Sample Page</title>
<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod()]
    public static string test()
    {
        return "q343242342342";
    }

</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
  function callMethod() {
      PageMethods.test(onSuccess, onFailure);
  }

  function onSuccess(result) {
      alert(result);
  }

  function onFailure(error) {
      alert('fail');
  } 
         </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
 </asp:ScriptManager>
 <input type="button" id="btn" value="Click Me" onclick="callMethod();"  />
 </form>
 </body>
 </html>

This code worked for me - can't exactly tell where your issue is bu you don't have your code in a runat=server block (assuming you are just copying from code behind maybe). But this exact code should work - I think.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<html>
<head runat="server">
<title>Sample Page</title>
<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod()]
    public static string test()
    {
        return "q343242342342";
    }

</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
  function callMethod() {
      PageMethods.test(onSuccess, onFailure);
  }

  function onSuccess(result) {
      alert(result);
  }

  function onFailure(error) {
      alert('fail');
  } 
         </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
 </asp:ScriptManager>
 <input type="button" id="btn" value="Click Me" onclick="callMethod();"  />
 </form>
 </body>
 </html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文