ASP.NET Web 表单 [WebMethod] 未被 AJAX Post 调用

ASP.NET Web Forms [WebMethod] not being called by AJAX Post

我正在使用 bootstrap/jquery 3.4.1 js 开发 ASP.NET Web 表单项目。我的 AJAX Post 无法正常工作。它应该在 Schedule.aspx 内命中我的 [WebMethod],但事实并非如此。我已经在它上面设置了一个断点,并且在单击保存按钮时它从未被激活。 AJAX 成功并弹出警报,我已经验证了 stringify 数据按预期输出,但为什么它没有命中 [WebMethod]?

这是我的 JavaScript 函数:

$(document).on('click', '#modalSave', function (e) {
     
    var testValue = "TestValue";

    $.ajax({
        type: "POST",
        url: "Schedule.aspx/InsertItem",
        data: JSON.stringify({ Content: testValue }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function ()
        {
            alert("AJAX success");
            $('#DetailsModal').modal('hide');
        }
    })
});

我的 [WebMethod]

using System.Web.Services;

namespace Scheduler
{
    public partial class Schedule : Page
    {
        [WebMethod]
        public static string InsertItem(string Content)
        {
            return Content;
        }
    }
}

我的模式和保存按钮:

<div id="DetailsModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;  </button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
                <textarea id="modalTextArea" style="width: 100%; height: 300px;"></textarea>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button id="modalSave" type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

我已经模拟了你的代码,我可以成功进入WebMethod的断点,一个可能的原因是你没有在你的[中激活调试模式 =25=]Visual StudioIDE

更新 2:

使用 RouteConfig 配置启用对 WebMethod 的调用:

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