使用asp.net aspx的Ajax返回Undefined

时间:2015-05-04 11:52:10

标签: asp.net ajax json 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>

2 个答案:

答案 0 :(得分:1)

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) {
            }
        });

答案 1 :(得分:0)

从你的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

中找到更多详情