ASP .NET: Cannot call Page WebMethod using jQuery
        Posted  
        
            by John
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by John
        
        
        
        Published on 2010-05-04T20:44:02Z
        Indexed on 
            2010/05/04
            20:48 UTC
        
        
        Read the original article
        Hit count: 247
        
I created a WebMethod in the code-behind file of my page as such:
[System.Web.Services.WebMethod()]
public static string Test()
{
    return "TEST";
}
I created the following HTML page to test it out:
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/></script>
    <script type="text/javascript">
        function test() {            
            $.ajax({
                type: "POST",
                url: "http://localhost/TestApp/TestPage.aspx/Test",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "text",
                success: function(msg) {
                    alert(msg.d);
                }
            });
        }
    </script>
</head>
<body>
    <button onclick="test();">Click Me</button>
</body>
</html>
When I click the button, the AJAX fires off, but nothing is returned. When I debug my code, the method Test() doesn't even get called. Any ideas?
© Stack Overflow or respective owner