Unexpected token u in JSON

user2896120

I have an Ajax function that sends and retrieves a string. Once the Ajax receives the string, it calls a function called drawChart. drawChart then converts that string into an array using JSON.parse I am also using Google Charts to create a chart using the data that was passed in. Everything is working correctly. The chart is being made and it looks all fine, however; I am getting this error in the console:

Uncaught (in promise) SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at drawChart (index.js?c1496931740:24) at anonymous

I did research this error and it happens when the data type that is being passed in JSON.parse is undefined. I tried to use console.log(typeof(data)) to check if it really is undefined, but it outputs 2 things. First it outputs string, then outputs undefined. Afterwards, it displays the error, but the chart gets displayed successfully. Here's my code:

function aggregator(field, size) {
    /*Build jSON query string*/
    var jString = '{"aggs": { "group_by_date": { "terms": { "field": "' + field + '", "size" : ' + size + '} } } }';

    google.charts.setOnLoadCallback(drawChart);   /*Load Chart*/

    /*Sending and retrieving data using Ajax*/
    $.ajax({
        url: 'index.php',
        type: 'post',
        datatype: 'json',
        data: {
            qSet: jString,
            size: size
        },
        success: drawChart
    });
}

var drawChart = function(data)
{   
    console.log(typeof(data));
    data = JSON.parse(data);  /*Convert the data back into an array*/

    var chartData = new google.visualization.DataTable();  /*Set up the chart*/

    chartData.addColumn('string', 'year'); 
    chartData.addColumn('number', 'Records');

    /*For loop to add rows*/
    for (var i = 0; i < data.length; ++i){
        chartData.addRow(data[i]);
    }

    var options = {
        title: 'Arrival Data'
    };

    var chart = new google.visualization.BarChart(document.getElementById('chartContainer'));
    chart.draw(chartData, options);
};

What is the reason for this error? Why do I receive 2 outputs (string and then undefined) when I do console.log(typeof(data))? How can I fix it?

Quentin

Why do I receive 2 outputs (string and then undefined) when I do console.log(typeof(data))?

Because you are calling the function twice.

  1. google.charts.setOnLoadCallback(drawChart);
  2. success: drawChart

Only call it when you get the data.


google.charts.setOnLoadCallback lets you specify a function to run when the API is available.

Create a new function which makes your Ajax request and call that when the API has loaded.

google.charts.setOnLoadCallback(sendAjaxRequest); /*Load Chart*/

function sendAjaxRequest() {

  $.ajax({
    url: 'index.php',
    type: 'post',
    datatype: 'json',
    data: {
      qSet: jString,
      size: size
    },
    success: drawChart
  });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

"Unexpected token u in JSON at position 0"

React - Unexpected token u in JSON at position 5

Unexpected token: u JSON.parse() issue

Unexpected token u in JSON at position 0 at <Jasmine>

undefined:1 undefined ^ SyntaxError: Unexpected token u in JSON at position 0

body-parser, Unexpected token u in JSON at position

Uncaught SyntaxError: Unexpected token u in JSON at position 0

Saving to localstorage - SyntaxError: Unexpected token u in JSON at position 0

Unexpected token u in JSON at position 0 not working for async

Unexpected token u in JSON at position 0 With Angularjs POST method

Unexpected token u in JSON at position 0 (but only sometimes)

Unexpected token U in JSON at position 0 - Not able to store data

Uncaught Promise Rejection SyntaxError: Unexpected token u in JSON at position 0

React Native - Unexpected token U in JSON at position 0

LESS Compiler: Unexpected token u

Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json

SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse in angular8

VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)

ERROR SyntaxError: Unexpected token u in JSON at position 0at JSON.parse (<anonymous>)

curl: Unexpected token in JSON

Unexpected token with valid json?

Unexpected token { in JSON

Unexpected token D in json?

Unexpected token # in JSON

JSON SyntaxError: Unexpected token <

Get json unexpected token

Getting SyntaxError: Unexpected token u in JSON at position 0 when running Lambda Test

How do I fix: SyntaxError: Unexpected token u in JSON at position 1

TypeScript error TS5014: Unexpected token u in JSON at position 0