我有一个页面,这个方法在CreateTicket.aspx.cs:为什么JavaScript调用Page WebMethod会导致“500:未知的Web方法”?
[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
});
}
上运行它萤火虫,我在控制台上得到以下错误:
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
为什么会发生这种情况?
这是怎么发生的? – 2009-08-28 18:10:25
可能是CopyAndPasteException。 – 2009-08-28 18:33:50