我试图在我的一个aspx页面文件中调用以下webmethod:
[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
中。破解点甚至没有被网络方法击中,并返回整个页面的内容。知道是什么引起了这个吗?
编辑: 使用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" />
P.S。刚刚意识到我正在iFrame中调用它。这可能与此问题有关吗?
答案 0 :(得分:1)
我认为你需要明确添加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);
}
});
答案 1 :(得分:0)
从static
方法中删除GetReportDetails
关键字。
答案 2 :(得分:0)
不要在Web方法中编写管道代码(JSON序列化/反序列化)。简单地获取/返回模型(.NET POCO对象):
[WebMethod]
public static string GetReportDetails()
{
var reportDetails = DataAccess.Database().GetBusinessInterestReportDetails(HttpContext.Current.User.Identity.Name);
return reportDetails;
}
然后消费:
$.ajax({
type: 'POST',
url: 'SummaryReport.aspx/GetReportDetails',
contentType: 'application/json',
success: function (data) {
// the actual object will be inside data.d
var reportDetails = data.d;
// Now you could directly use the properties of your model
// For example if your ReportDetails .NET type had a string property
// called FooBar you could directly alert(reportDetails.FooBar);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('An error has occured: ' + errorThrown);
}
});
需要注意的事项:
contentType: 'application/json'
以向Web方法指示我们要将JSON用作传输机制dataType: 'json'
属性,因为jQuery足够智能,可以使用Content-Type响应头并自动解析传递给data
回调的success
参数。 我还建议您阅读有关consuming ASP.NET PageMethods directly from jQuery
的文章。
答案 3 :(得分:0)
修正了它。事实证明,aspx文件顶部缺少inherits属性。
所以现在我有:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SummaryReport.aspx.cs" MasterPageFile="~/MasterPages/SummaryReports.Master"
Inherits="Web.SummaryReport"
%>
答案 4 :(得分:0)
我想根据我在这个问题上的经验做出贡献。 如果您使用的是内容页面或母版页,则无法通过访问该页面来调用WebMethod,而是添加webserivce页面并使用它,请参阅它与我合作的链接。 http://forums.asp.net/post/5822511.aspx
我在这里复制了
继承System.Web.Services.WebService
<WebMethod()> _
Public Function MyFunction() As String
Return "Hello World"
End Function
然后您可以从主页面或内容页面调用webmethod,如下所示:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function MyFunction() {
var request = $.ajax({
type: 'POST',
url: 'HelloWord.asmx/MyFunction',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (serverdata) {
alert("Done " + serverdata);
},
error: function (error) {
alert("error ");
}
});
return false;
}
</script>
请务必取消注释: System.Web.Script.Services.ScriptService 在webservice页面中,因为在创建页面时它将被注释