4

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!";
    }
}
}
15
  • I think you are missing WebMethod parenthesis (WebMethod()) Commented Oct 7, 2015 at 16:16
  • Did you try using the full path in the URL param? Commented Oct 7, 2015 at 16:17
  • This is a resting service right? If so try [WebGet(UriTemplate = "Announcements.aspx/AddAnnouncement/{args=null}")] in place of WebMethod Commented Oct 7, 2015 at 16:18
  • @Viru - No it's not required, WebMethod is not a method it is an attribute. Commented Oct 7, 2015 at 16:18
  • @W3AVE: Just tried it. Doesn't help. Commented Oct 7, 2015 at 16:19

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

0

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"
                    }
                });

Comments

-2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.