【问题标题】:Why is JSON Object for Ajax throwing an error, but json file doesn't?为什么 Ajax 的 JSON 对象会抛出错误,而 json 文件不会?
【发布时间】:2020-02-05 16:39:54
【问题描述】:

我尝试提供一个 JSON 对象以显示在谷歌图表中。

当我在 ajax 中调用 json 文件时,我可以显示一个谷歌图表,但是当我尝试从代码隐藏中获取数据时,我得到一个错误。

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetData()
    {

        var chartData = new object[10+ 1];
        int j = 0;
        for (int i=1; i <= 10; i++)
        {

            chartData[j] = new object[] { i.ToString(), i*i, i};
            j++;
        }
        //return chartData;
        string json = JsonConvert.SerializeObject(chartData);
             return json;
     }

我在这里得到的 Json 字符串根据 json 格式化程序是有效的。 "[[\"1\",1,1],[\"2\",4,2],[\"3\",9,3],[\"4\",16,4], [\"5\",25,5],[\"6\",36,6],[\"7\",49,7],[\"8\",64,8],[\ "9\",81,9],[\"10\",100,10]]"

所以我有字符串、数字、数字作为数组。

我的调用看起来像这样(我从https://www.encodedna.com/google-chart/create-line-charts-with-dynamic-json-data-using-google-charts.htm 得到它),如果我使用 json 文件作为数据,这工作正常,当我尝试使用它后面的代码中的数据时会跳转到错误:

<script>
// Visualization API with the 'corechart' package.  url: "data.json",
google.charts.load('visualization', { packages: ['corechart'] });
google.charts.setOnLoadCallback(drawLineChart);

function drawLineChart() {
    $.ajax({
        url: "Chart.aspx/GetData",
        dataType: "json",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var arrSales = [['RejectReason', 'Count', 'Perc. (%)']];    // Define an array and assign columns for the chart.

            // Loop through each data and populate the array.
            $.each(data, function (index, value) {
                arrSales.push([value.RejectReason, value.Count, value.Perc]);
            });

            // Set chart Options.
            var options = {
                title: 'Sorting',
                curveType: 'function',
                legend: { position: 'bottom', textStyle: { color: '#555', fontSize: 14 } }  // You can position the legend on 'top' or at the 'bottom'.
            };

            // Create DataTable and add the array to it.
            var figures = google.visualization.arrayToDataTable(arrSales)

            // Define the chart type (LineChart) and the container (a DIV in our case).
            var chart = new google.visualization.LineChart(document.getElementById('chart'));
            chart.draw(figures, options);      // Draw the chart with Options.
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('Got an Error');
        }
    });
}

我希望后面的代码显示在谷歌图表中。稍后我想使用从数据库中提取和丰富的数据来显示,所以我不会从 json 文件访问它。

【问题讨论】:

  • “它跳入错误” - 我们无法猜测错误是什么。按 F12 并阅读它,然后研究它。
  • 对不起,我的意思是它调用错误:函数而不是成功:函数。所以我猜这是因为我的 JSON 对象中的解析错误
  • 别猜了。检查错误。此外,错误回调是针对 HTTP 错误调用的,而不是针对 JSON 语法错误的。
  • 我刚刚做了:alert(errorThrown.toString());警报(textStatus.toString());警报(XMLHttpRequest.toString());这给了我:空错误对象对象我如何获得更多信息?好的,使用 responseText: {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
  • 是的,所以你有 HttpOnly 身份验证 cookie。

标签: c# asp.net json ajax


【解决方案1】:

这是错误的 JSON 数据

您应该避免 JSON 字符串中的 "\

[[1,1,1],[2,4,2],[3,9,3],[4,16,4],[5,25,5],[6,36,6],[7,49,7],[8,64,8],[9,81,9],[10,100,10]]

您可以使用从 JSON 格式化程序复制的代码,它仅用于查看目的。您的 JSON 文件包含正确的 JSON。我认为这不是人类可读的格式。

【讨论】:

  • 它是 Visual Studio C# 调试器的字符串可视化器,它显示了这样的 JSON 字符串。
  • 是的,你可以。它是 C# 中的字符串,以双引号开头和结尾,C# 字符串中的双引号用反斜杠转义。那是字符串的 C# 视图,这不是通过网络传输的。
猜你喜欢
  • 2023-02-04
  • 1970-01-01
  • 1970-01-01
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 2013-02-18
相关资源
最近更新 更多