Why does Javascript calling the WebMethod page result in "500: Unknown Web Method"?

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 the 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&amp; completedSynchronously) +75

Why is this happening?

+3
source share
3 answers

I solved this problem.

What's happening? Something stupid (as usual):

  • The CreateTicket.aspx page declaration does not have an Inherits attribute, so CreateTicket.aspx.cs was not connected as a partial class, even using the CodeBehind attribute.
+6
source

Is CreateTicket.aspx from WebService called?

, ScriptService, .NET , JavaScript.

. -, WCF. WCF -.

0

.NET 3.5 , WCF.

CodeProject , OperationsContract DataContract , .

0

Source: https://habr.com/ru/post/1716334/


All Articles