-1

I try to give a JSON object to display in a google chart.

I can show a google chart when i call in the ajax the json file, but when i try to get the data from codebehind, i get an error.

    [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;
     }

The Json string i get here is valid according to a json formatter. "[[\"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]]"

So i have string, number, number as an array.

My call looks like this (I have it from https://www.encodedna.com/google-chart/create-line-charts-with-dynamic-json-data-using-google-charts.htm) and this works fine if i use a json file for the data, when i try to use the data from code behind it jumps into the error:

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

I want the code behind to be shown in the google chart. Later i want to use extracted and enriched data from a database to be shown, so i will not access it from a json file.

Monika_Aydt
  • 111
  • 1
  • 1
  • 5
  • _"it jumps into the error"_ - we can't guess what the error is. Hit F12 and read it, then research it. – CodeCaster Oct 08 '19 at 11:36
  • Sorry, I mean it calls the error: function and not the success: function. So i guess it is because of a parsing error in my JSON object – Monika_Aydt Oct 08 '19 at 11:41
  • Don't guess. Inspect the error. Also, the error callback is called for an HTTP error, not for a JSON syntax error. – CodeCaster Oct 08 '19 at 11:44
  • I just did: alert(errorThrown.toString()); alert(textStatus.toString()); alert(XMLHttpRequest.toString()); Which gives me: Empty error Object object How do i get more information? Ok did it with responseText: {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"} – Monika_Aydt Oct 08 '19 at 11:52
  • Yeah so you have HttpOnly auth cookies. – CodeCaster Oct 08 '19 at 11:56
  • Changed this by using this hint: https://stackoverflow.com/questions/23033614/asp-net-calling-webmethod-with-jquery-ajax-401-unauthorized settings.AutoRedirectMode = RedirectMode.Off; Thank you – Monika_Aydt Oct 08 '19 at 12:09

1 Answers1

-2

This is wrong JSON data

You should avoid the " and \ from your JSON string

[[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]]

you can use the copied code from JSON formatter, it is just made for view purposes only. Your JSON file contain correct JSON. i think it is not a human-readable format.

Justin J
  • 808
  • 8
  • 14
  • 1
    It is the Visual Studio C# debugger's string visualiser that shows the JSON string like that. – CodeCaster Oct 08 '19 at 11:45
  • Yes, you can. It is a string in C#, and that starts and ends with a double quote, and double quotes in a C# string are escaped by a backslash. That's the C# view of strings, and that is _not_ what goes over the wire. – CodeCaster Oct 08 '19 at 11:48