Reputation: 33471
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
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
Reputation: 75
For ajax success:
For me, it was helpful to make:
App_Start\RouteConfig
set from
settings.AutoRedirectMode = RedirectMode.Permanent;
to
settings.AutoRedirectMode = RedirectMode.Off;
public
static
using System.Web.Services;
and on top of method using just:
[WebMethod]
is enough
Upvotes: 1
Reputation: 76
Add public static
before your method...
ex.
[WebMethod]
public static string MethodName() {}
Upvotes: 3
Reputation: 15045
Clean the solution and rebuild. I've seen webmethods throw 500's until you do this.
Upvotes: 12