我在CreateTicket.aspx.cs中有一个包含此方法的页面:
[WebMethod()]
public static string Categories()
{
var business = new CategoryBusiness();
var categories = business.ListRootCategories();
return categories.Json();
}
页面上的javascript / jquery代码(同一页面,.aspx):
function LoadRootCategories() {
PageMethod("CreateTicket.aspx", "Categories", [], LoadCategoriesSucceded, LoadCategoriesFailed);
}
function PageMethod(page, fn, paramArray, successFn, errorFn)
{
//Create list of parameters in the form:
//{"paramName1":"paramValue1","paramName2":"paramValue2"}
var paramList = '';
if (paramArray.length > 0)
{
for (var i=0; i<paramArray.length; i+=2)
{
if (paramList.length > 0) paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i+1] + '"';
}
}
paramList = '{' + paramList + '}';
//Call the page method
$.ajax({
type: "POST",
url: page + "/" + fn,
contentType: "application/json; charset=utf-8",
data: paramList,
dataType: "json",
success: successFn,
error: errorFn
});
}
在firebug上运行它,我在控制台上收到以下错误:
500 Internal Server Error
Unknown web method Categories.
[ArgumentException: Unknown web method Categories.
Parameter name: methodName]
System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +517489
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
为什么会这样?
答案 0 :(得分:6)
我解决了这个问题。
发生了什么事?有点蠢(像往常一样):
答案 1 :(得分:0)
CreateTicket.aspx是否继承自WebService?
即使它确实如此,您的类也应该具有ScriptService属性,以便.NET生成其他类以帮助从JavaScript调用它。
注意:这仅适用于非WCF Web服务。 WCF添加了自己的Web服务属性。
答案 2 :(得分:0)
如果您使用的是.NET 3.5或更高版本,则还可以设置WCF服务。
CodeProject上有一个quick guide,关于如何在类上设置OperationsContract和DataContract注释以创建所述服务。