jquery ajax 调用未命中 C# WebMethod.点击页面加载

jquery ajax call not hitting c# webmethod. Hits page load though.

本文关键字:加载 WebMethod 调用 ajax jquery      更新时间:2023-09-26

我不知道为什么我的Javascript ajax调用不会命中我的c#方法。我在后面的代码中构建了它。

ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", @"swal.withForm({
                        title: 'Database Credentials',
                        formFields: [{
                            id: 'server',
                            placeholder: 'Server Name'
                        }, {
                            id: 'username',
                            placeholder: 'User Name'
                        }, {
                            id: 'password',
                            placeholder: 'Password',
                            type: 'password'
                        }, {
                            id: 'databaseName',
                            placeholder: 'Database Name'
                        }],
                        html: true,
                        confirmButtonText: 'Update'
                    }, function(isConfirm) {
                        console.log(JSON.stringify(this.swalForm));
                        $.ajax({
                            type: 'GET',
                            url: 'WebMethods.aspx/CheckCreateDatabase',
                            contentType: 'application / json; charset = utf - 8',
                            dataType: 'html',
                            data: JSON.stringify(this.swalForm),
                            success: function(data) {
                                swal('Success ' + data);
                            },
                            error: function(data, success, error) {
                                swal('Error ' + error);
                            }
                        });
                    });", true);

控制台根据我的输入输出 json 字符串的此结果。

{"server":"dfd","username":"df","password":"dfd","databaseName":"dfd"}

我的代码隐藏

    public partial class WebMethods : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var a = "a";    
    }
[WebMethod]
    public static string CheckCreateDatabase(string server, string username, string password, string databaseName)
    {
        return "it worked";
    }
}

它将在页面加载事件中命中我的断点,但不会在 CheckCreateDatabase 块中命中它。

我尝试了一堆不同的帖子,但仍然无法让它达到这种方法。有没有人看到任何错误或我可以调查的任何事情。谢谢。

我想通了。我从一个网站复制了内容类型,里面有空格。这把事情搞砸了。