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