28

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?

5 Answers 5

101

Your web method needs to be public and static.

Sign up to request clarification or add additional context in comments.

4 Comments

Your web method needs to be declared as "static"
@Max -- correct, it needs to be both public and static, though in this question it already was static.
I was just guessing that Victor's problem was forgetting the static.
Note that is not true for web methods in an asmx, where they have to be public and not static. See stackoverflow.com/questions/4543347/… and stackoverflow.com/questions/3158678/…
12

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

Comments

3

Add public static before your method...

ex.

[WebMethod]
public static string MethodName() {}  

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.