Search code examples
asp.netajaxpagemethods

Asp.net ajax pagemethods not working


I have a project that originally did not use ajax. Now I want to make the page ajax enabled. What I did already is Install the ajaxToolkit and replace the web.config content with another one from an ajax enabled web site. I added Script Manager:

<form id="Form1"  runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" EnableScriptGlobalization="True" />

my method in the .cs file looks like this

[WebMethod]
public static string[] CheckRating(string[] details)
{
    string[] result = new string[] { "AAAAAAAAAAA", "" };
    return result;
}

my java script functions are:

 function someName() {

        var details = new Array();
        details[0] = $("#HiddenFieldUsername").val();
        details[1] = "5310005003117";
        PageMethods.CheckRating(details, ss1, ss2);
    }
    function ss1(result, userContext, methodName)
    {
        $("#user_rating").show();
        $("#user_rating_text").html("Error User ratinh");
        alert("Sorry");
    }
    function ss2(error, userContext, methodName) {
        $("#user_rating").show();
        $("#user_rating_text").html("Error User ratinh");
        alert("Sorry");
    }

The problem is when it gets here: PageMethods.CheckRating(details, ss1, ss2); nothing happens. None of the code in ss1 or ss2 is executed... java scripts just stop working.


Solution

  • Weird, the following webform works perfectly fine for me:

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Web.Services" %>
    <script type="text/C#" runat="server">
        [WebMethod]
        public static string[] CheckRating(string[] details)
        {
            string[] result = new string[] { "AAAAAAAAAAA", "" };
            return result;
        }
    </script>
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <form id="Form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" EnableScriptGlobalization="True" />
        </form>
    
        <script type="text/javascript">
            var details = new Array();
            details[0] = 'foo';
            details[1] = 'bar';
            PageMethods.CheckRating(details, ss1, ss2);
    
            function ss1(result, userContext, methodName) {
                alert(result[0]);
            }
            function ss2(error, userContext, methodName) {
                alert("Error");
            }
        </script>
    </body>
    </html>
    

    Maybe you could use a javascript debugging tool such as FireBug to see exactly what happens under the covers and the actual AJAX request that is being sent as well as the response.