.NET Web 方法仅从 javascript 页面调用

.NET Web Method not being called from javascript only Page

我在 .aspx 页面的代码隐藏中有一个 web 方法,但是当我尝试访问它时,它不会仅触发该页面的 web 方法。 return状态为200,页面正在被调用,但是方法被忽略。如果我使用 name.aspx/GetData 或 name.aspx/Anything 并不重要,结果是 200,但方法没有被触发。我用 jquery Ajax 和 Postman 进行了测试。获得 Post 次尝试。 web.config 或任何其他内容有什么要更改的吗?

$.ajax({
                        url: '/adm/clientAccess.aspx/MyMethodInexistent',
                        data: {},
                        type: 'POST',
                        contentType: 'application/x-www-form-urlencoded',
                        dataType: 'html',
                        success: function (data) {
                           //I GET HERE even if the method doesn't exist, and if it exists, it doesn't return data.
                           alert(1);
                        },
                        error: function (response) {
                            alert(response.responseText);
                        }
                    }
                    );

需要使用 ScriptManager 元素启用页面方法:

<asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true"/>

你能试试下面的代码吗,确保方法的路径在下面的 url 中是正确的。如果您正在使用,还可以在脚本管理器级别启用页面方法。

   $.ajax({
    url: '/adm/clientAccess.aspx/MyMethod',
    data: {},
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: "true",
    success: function (data) {
       console.log(data);
       //alert(1);
    },
    error: function (response) {
        alert(response.responseText);
    }
}
);