Ajax 与 asp.net aspx returns 未定义

Ajax with asp.net aspx returns Undefined

我面临以下问题:每当我单击 button 时,alert 显示 undefined

网络方法:

    [WebMethod]
    public static string getTest()
    {
        return "testing success";
    }

Ajax 脚本

    <script type="text/javascript">
        function getTest() {
            $.ajax({
                type: "POST",
                url: "main.aspx/getTest",
                data: "{}",
                datatype: "json",
                contenttype: "/application/json; charset=utf-8",
                success: function (msg) {

                    alert(msg.d);

                },
                error: function (data) {
                }
            });
        }
    </script>

从您的 ajax 调用中删除这些

datatype: "json",
contenttype: "/application/json; charset=utf-8",

更多关于这些

Differences between contentType and dataType in jQuery ajax function

What is content-type and datatype in an AJAX request?

您可以在 jQuery Ajax documentation

中找到更多详细信息

datatype 应该是 dataTypecontenttype 应该是 contentType。同时从 "/application/json; charset=utf-8"

的开头删除 /
$.ajax({
            type: "POST",
            url: "main.aspx/getTest",
            data: "{}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (msg) {

                alert(msg.d);

            },
            error: function (data) {
            }
        });