请参阅AJAX返回的JSON

时间:2017-01-09 12:44:48

标签: json ajax

请参阅以下代码:

<script type="text/javascript"  src="Javascript/json2.js"></script>
    <script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script>
    <script type = "text/javascript">
        function GetData() {
            $.ajax({
                type: "POST",
                url: "JSONExample.aspx/GetPerson",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess(),
            //async: false,
            failure: function (response) {
                alert('there was an error counting possibles')
            }
        });

        function OnSuccess() {
            return function (response) {
                alert(response.d);
            }
        }
        }
        GetData()
    </script>

我相信response.d会返回被消毒的JSON。我如何看待JSON,以便我可以将它自己解压缩为.NET对象?

2 个答案:

答案 0 :(得分:1)

尝试用您的代码替换此代码,如果您已在此网址上以json格式打印数组JSONExample.aspx/GetPerson

如果你还没有在json中打印数组,那么下面的代码将不起作用。

<script type = "text/javascript">
        function GetData() {
            $.ajax({
                type: "POST",
                url: "JSONExample.aspx/GetPerson",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess(response),
            //async: false,
            failure: function (response) {
                alert('there was an error counting possibles')
            }
        });

        function OnSuccess(response) {
            return function (response) {
            var data = JSON.parse(response);
                console.log(data);
            }
        }
        }
        GetData()
    </script>

您将能够在开发人员工具的控制台中看到响应。

答案 1 :(得分:0)

试试这个:

<script type="text/javascript"  src="Javascript/json2.js"></script>
    <script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script>
    <script type = "text/javascript">
        function GetData() {
            $.ajax({
                type: "POST",
                url: "JSONExample.aspx/GetPerson",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            //async: false,
            failure: function (response) {
                alert('there was an error counting possibles')
            }
        });

        function OnSuccess(data) {
            alert(data);
        }
        GetData()
    </script>

此外,您可以使用Internet浏览器的开发工具调试信息(默认情况下为F12)。