【发布时间】:2012-12-08 02:13:58
【问题描述】:
我正在尝试调用在我的一个 aspx 页面文件中找到的以下 webmethod:
[WebMethod]
public static string GetReportDetails()
{
var reportDetails = DataAccess.Database().GetBusinessInterestReportDetails(HttpContext.Current.User.Identity.Name);
var json = BusinessInterestReport.GetJson(reportDetails);
return json;
}
这是我用来调用 webmethod 的 javascript:
$.ajax({
type: 'POST',
url: 'SummaryReport.aspx/GetReportDetails',
dataType: 'json',
success: function (data) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('An error has occured: ' + errorThrown);
}
});
进行此 ajax 调用的 javascript:
$('.reportOption').click(function (e) {
$.ajax({
type: 'POST',
url: 'SummaryReport.aspx/GetReportDetails',
dataType: 'json',
success: function (data) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('An error has occured: ' + errorThrown);
}
});
})
ScriptModule 配置已经在web.config 中。断点甚至没有在 webmethod 上被击中,而是返回了整个页面的内容。知道是什么原因造成的吗?
编辑: 使用 Chrome 的调试控制台我发现了这个错误:
[ArgumentException: Unknown web method GetReportDetails.
Parameter name: methodName]
System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +516665
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +168
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
为什么不选择方法名?我还使用 <asp:ScriptManager ID="smMain" runat="server" EnablePageMethods="true" /> 启用了 PageMethods
附:刚刚意识到我是从 iFrame 中调用它的。这和问题有关系吗?
【问题讨论】:
-
如果 webmethod 没有被点击,它是否返回了
500或404错误页面的代码? -
请显示包含您的按钮(或其他)的页面的 HTML
-
变量json包含什么?
-
我见过的大多数例子包括
contentType: 'application/json; charset=utf-8'。这可能与它有关吗? -
您说您是在您的“aspx”文件中调用它 - 您是否在 ScriptManager 中启用了 PageMethods?
标签: c# jquery asp.net ajax webmethod