猿问

AJAX Post 未调用 ASP.NET Web 窗体 [WebMethod]

我正在使用 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>


茅侃侃
浏览 136回答 1
1回答

开满天机

我已经模拟了你的代码,我可以成功进入断点WebMethod,一个可能的原因是你没有在你的Visual Studio IDE中激活调试模式更新 2:WebMethod使用 RouteConfig 配置启用调用:public static class RouteConfig{&nbsp; &nbsp; public static void RegisterRoutes(RouteCollection routes)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var settings = new FriendlyUrlSettings();&nbsp; &nbsp; &nbsp; &nbsp; settings.AutoRedirectMode = RedirectMode.Off;&nbsp; &nbsp; &nbsp; &nbsp; routes.EnableFriendlyUrls(settings);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答