Q: Unable to access webmethod on aspx.cs page I am trying to call a webmethod with jquery ajax. However, the call returns a Not Found error. Trying to access the method directly via the URL also returns a 404 error.
I made sure to add EnablePageMethods="true" parameter to the <asp:ToolkitScriptManager> on the master page.
Announcements.aspx:
<script type="text/javascript">
    $(function () {
        $("#CreateBtn").click(function () {
            var announce = {};
            announce["title"] = "An Announcement";
            announce["body"] = "Announcement Body";

            $.ajax({
                type: "POST",
                url: "Announcements.aspx/AddAnnouncement",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(announce),
                success: function () {
                    alert("success!");
                },
                error: function (x, t, e) {
                    alert(t); //alerts "error"
                    alert(e); //alerts "Not Found"
                }
            });
            return false;
        })
    });
</script>

Announcements.aspx.cs
using System.Web.Services;

namespace MyProject.ContentTools
{   
public partial class Announcements : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [WebMethod]
    public static string AddAnnouncement(string title, string body)
    {
        var newTitle = title;
        var newBody = body;

        return "it worked!";
    }
}
}


A: If you are using PageMethods from within an ASP.NET MVC project, you probably need to ignore the routes for aspx pages (and, therefore, the PageMethod urls that are based on them). In your route registration (usually at App_Start/RouteConfig.cs), add the following line:
routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });

This should allow the PageMethod request to go through without interference from MVC routing.

A: i'm not sure about the 404 problem exactly, but a couple other things:


*

*you're using <asp:ToolkitScriptManager>. this should be a <asp:ScriptManager> (makes a difference??);

*if using jquery's ajax, i think the <asp:ScriptManager> is not even required;


A: Try this..You have to add ScriptMethod notation in your method...
[WebMethod]    
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
        public static string AddAnnouncement(string title, string body)
        {
            var newTitle = title;
            var newBody = body;

            return "it worked!";
        }
and in your ajax method try to change data format.

    $.ajax({
                    type: "POST",
                    url: "Announcements.aspx/AddAnnouncement",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(title: announce.title, body: announce.body),
                    success: function () {
                        alert("success!");
                    },
                    error: function (x, t, e) {
                        alert(t); //alerts "error"
                        alert(e); //alerts "Not Found"
                    }
                });


A: Try this..You have to call PageMethods like below in your jquery..
<script type="text/javascript">
    $(function () {
        $("#CreateBtn").click(function () {

            var title = "An Announcement";
            var body = "Announcement Body";
             PageMethods.AddAnnouncement(title,body,success,error);
        function success(result) {
                    alert("success!");
                }
        function error(result) {
                    alert(result); 
                }
            });
            return false;
        })
    });
</script>

