Mark Struzinski
Mark Struzinski

Reputation: 33471

ASP.NET jQuery error: Unknown Web Method

This is my first time attempting to call an ASP.NET page method from jQuery. I am getting a status 500 error with the responseText message that the web method cannot be found. Here is my jQuery $.ajax call:

function callCancelPlan(activePlanId, ntLogin) {
    var paramList = '{"activePlanId":"' + activePlanId + '","ntLogin":"' + ntLogin + '"}';
    $.ajax({
        type: "POST",
        url: "ArpWorkItem.aspx/CancelPlan",
        data: paramList,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function() {
            alert("success");
        },
        error: function(xml,textStatus,errorThrown) {
            alert(xml.status + "||" + xml.responseText);
        }
    });
}

And here is the page method I am trying to call:

[WebMethod()]
private static void CancelPlan(int activePlanId, string ntLogin)
{
    StrategyRetrievalPresenter presenter = new StrategyRetrievalPresenter();
    presenter.CancelExistingPlan(offer, ntLogin);            
}

I have tried this by decorating the Web Method with and without the parens'()'. Anyone have an idea?

Upvotes: 28

Views: 36908

Answers (5)

Priyank Raunak
Priyank Raunak

Reputation: 11

First Of All Don't Forget To Include using System.Web.Services;

And Make Sure Your Method Should Be Public And Static and avoid adding Multiple Scripts in same Page like jquerymin.js shouldn't be used for every Function/Method in same Page

[WebMethod] public static sting MethodName(){}

I Had The Same Issue Which Using Ajax And Jquery To Check Username Exists Or Not.

Upvotes: 0

JustSomeGuyInWorld
JustSomeGuyInWorld

Reputation: 75

For ajax success:

For me, it was helpful to make:

  1. App_Start\RouteConfig

    set from

settings.AutoRedirectMode = RedirectMode.Permanent;

to

settings.AutoRedirectMode = RedirectMode.Off;
  1. make your using method:
public

static
  1. add:
using System.Web.Services;

and on top of method using just:

[WebMethod] 

is enough

Upvotes: 1

Tukaram
Tukaram

Reputation: 76

Add public static before your method...

ex.

[WebMethod]
public static string MethodName() {}  

Upvotes: 3

tvanfosson
tvanfosson

Reputation: 532435

Your web method needs to be public and static.

Upvotes: 101

Cory House
Cory House

Reputation: 15045

Clean the solution and rebuild. I've seen webmethods throw 500's until you do this.

Upvotes: 12

Related Questions