Tell me where I made a mistake, that I get an error: "Now the error is: Uncaught SyntaxError: Unexpected token o in JSON at position 1"
Here is a piece of code where the problem is:
$(function () { $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", dataType: 'json', url: '/Chart/GetVotings', }).data (function (votings) { votings = $.parseJSON(votings); $: each(votings, function (index, data) { chartData.labels.push(data.name); chartData.datasets[0].data.push(data.voice); }); }); }); Here is the whole code:
<canvas id="ChartVote" width="300" height="200"></canvas> <script type="text/javascript" charset="utf-8"> $(function () { $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", dataType: 'json', url: '/Chart/GetVotings', }).done(function (votings) { console.log(votings); votings = $.parseJSON(votings); $: each(votings, function (index, data) { chartData.labels.push(data.name); chartData.datasets[0].data.push(data.voice); }); }); }); var ctx = document.getElementById("ChartVote"); var myChart = new Chart(ctx, { type: 'bar', data: { labels: [], datasets: [{ label: '# of Votes', data: [], backgroundColor: [ 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)' ], borderColor: [ 'rgba(255,99,132,1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)' ], borderWidth: 1 }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } }); </script>
votings = $.parseJSON(votings);? Addconsole.log(votings);before this line and insert what is displayed in the console, in your question. - IgorChartController.GetVotings. - Igor