Search code examples
jqueryasp.netajaxpostwebmethod

jQuery AJAX doesn't hit WebMethod


You might wonder it as a duplicate post. But, I have researched a lot on various blog but I could not understand what the mistake I 'm doing.

I'm calling a web method as below

 $.ajax({
    type: "POST",
    url: "test.aspx/Save",
    data: "{ 'Id': 'test', 'newvalue': 'test' }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function () {
        alert('success');
    },
    error: function (xhr, status, error) {
        alert(err.Message);
    },
    failure: function () {
        alert('failure');
    }
});

I get the alert "Success" but I never see it hitting WebMethod. My web method is as below

[WebMethod]
    public static void Save(string Id, string newvalue)
    {
        //my code

    }

I kept breakpoint but it never hits. I also tried with $Post but same issue

 $.post("test.aspx/Save", { Id: "test", newvalue: "test" }, function (data, status) {
    alert('second');
});

None of these calls do hit web method but I get Alert of Success in Ajax call and Alert of "second" in the post call. Please advise, if anyone of you have any clue. Thanks


Solution

  • I have got a solution to this. In my RouteConfig.cs, RedirectMode is not set correctly. After changing mode to off, everything worked.

    public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Off;
            routes.EnableFriendlyUrls(settings);
        }