admin管理员组文章数量:1317898
I have a page with this method in CreateTicket.aspx.cs:
[WebMethod()]
public static string Categories()
{
var business = new CategoryBusiness();
var categories = business.ListRootCategories();
return categories.Json();
}
And the javascript/jquery code on the page (same page, .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
});
}
Running it on firebug, I get the following error on console:
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& pletedSynchronously) +75
Why is this happening?
I have a page with this method in CreateTicket.aspx.cs:
[WebMethod()]
public static string Categories()
{
var business = new CategoryBusiness();
var categories = business.ListRootCategories();
return categories.Json();
}
And the javascript/jquery code on the page (same page, .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
});
}
Running it on firebug, I get the following error on console:
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& pletedSynchronously) +75
Why is this happening?
Share Improve this question edited Aug 28, 2009 at 17:36 Powerlord 88.8k17 gold badges129 silver badges177 bronze badges asked Aug 28, 2009 at 17:11 Victor RodriguesVictor Rodrigues 11.8k25 gold badges78 silver badges108 bronze badges3 Answers
Reset to default 6I resolved this problem.
What was happening? Something stupid (as usual):
- 'Inherits' attribute was missing in the CreateTicket.aspx page declaration, so the CreateTicket.aspx.cs wasn't bound as the partial class, even using the CodeBehind attribute.
Does CreateTicket.aspx inherit from WebService?
Even if it does, your class should also have the ScriptService attribute on it, so that .NET generates additional classes to assist in calling it from JavaScript.
Note: This only applies to non-WCF web services. WCF adds in its own attributes for doing web services.
If you have .NET 3.5 or newer, you can also set up a WCF service.
There's a quick guide on CodeProject on how to set up the OperationsContract and DataContract annotations on your classes in order to create said service.
版权声明:本文标题:asp.net - Why Javascript calling to Page WebMethod results in "500: Unknown web method"? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742035897a2417289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论