I am trying to call a web method with jquery ajax. However, the call returns a Not Found error. Attempting to access the method directly through the URL also returns a 404 error.
I have definitely added the option EnablePageMethods="true"
to <asp:ToolkitScriptManager>
on the main 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);
alert(e);
}
});
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!";
}
}
}
source
share