views:

206

answers:

2

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"&gt;&lt;/script&gt;
<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"/>
+1  A: 

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"&gt;&lt;/script&gt;
<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>
brendan
yea for me it returns fail and I've done exactly what you've done.
Jreeter
A: 

Ok........ So apparently Chrome is acting up, this works fine in IE.

Jreeter