[英]Unable to access webmethod on aspx.cs page
I am trying to call a webmethod with jquery ajax. 我试图用jquery ajax调用web方法。 However, the call returns a Not Found error.
但是,该调用返回Not Found错误。 Trying to access the method directly via the URL also returns a 404 error.
尝试直接通过URL访问该方法也会返回404错误。
I made sure to add EnablePageMethods="true"
parameter to the <asp:ToolkitScriptManager>
on the master page. 我确保将
EnablePageMethods="true"
参数添加到母版页上的<asp:ToolkitScriptManager>
。
Announcements.aspx : 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 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!";
}
}
}
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). 如果您在ASP.NET MVC项目中使用PageMethods,则可能需要忽略aspx页面的路由(以及因此基于它们的PageMethod URL)。 In your route registration (usually at
App_Start/RouteConfig.cs
), add the following line: 在路由注册中(通常在
App_Start/RouteConfig.cs
),添加以下行:
routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
This should allow the PageMethod request to go through without interference from MVC routing. 这应该允许PageMethod请求通过而不受MVC路由的干扰。
i'm not sure about the 404 problem exactly, but a couple other things: 我不确定404问题,但还有其他几件事:
<asp:ToolkitScriptManager>
. <asp:ToolkitScriptManager>
。 this should be a <asp:ScriptManager>
(makes a difference??); <asp:ScriptManager>
( <asp:ScriptManager>
??); <asp:ScriptManager>
is not even required; <asp:ScriptManager>
; Try this..You have to add ScriptMethod notation in your method... 试试这个..你必须在你的方法中添加ScriptMethod表示法...
[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"
}
});
Try this..You have to call PageMethods like below in your jquery.. 试试这个..你必须在你的jquery中调用如下的PageMethods ..
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.