我正在尝试调用在我的一个 aspx 页面文件中找到的以下网络方法:
[WebMethod]
public static string GetReportDetails()
{
var reportDetails = DataAccess.Database().GetBusinessInterestReportDetails(HttpContext.Current.User.Identity.Name);
var json = BusinessInterestReport.GetJson(reportDetails);
return json;
}
这是我用来调用网络方法的 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
中.断点甚至没有在 web 方法上被击中,整个页面的内容都被返回。知道是什么原因造成的吗?
编辑: 使用 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 中调用它的。这与问题有什么关系吗?
最佳答案
我认为您需要显式添加contentType
,因为它的默认值是application/x-www-form-urlencoded; charset=UTF-8
,这不是你想要的。
所以您可能想稍微修改一下您的 jQuery 代码。
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "SummaryReport.aspx/GetReportDetails",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('An error has occured: ' + errorThrown);
}
});
关于c# - 为什么这个 AJAX 调用返回整个页面内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13974202/