Unexpected token ':'

Unexpected token ':'

mariolcvmariolcv Posts: 3Questions: 0Answers: 0

I get this error console when the data in json format is load. JSON data is created by a php function:

private function createJSON()
    {
        $array_data = '{"data":[';

        for ($i = 0; $i < count($this->data); $i++){
            $array_data .= '[';
            for ($j= 4; $j < count($this->data[$i]); $j++){
                if ($j == 5){
                    $j += 2;
                }
                $array_data .= '"'.$this->data[$i][$j].'",';
            }
            $array_data = substr($array_data, 0, -1);
            $array_data .= '],';
        }
        $array_data = substr($array_data, 0, -1);
        $array_data .= ']}';
        $this->json_data = $array_data;
        $path = 'javascript/json_data.json';
        file_put_contents($path, $this->json_data);
    }

and the output is: (for example)

{"data":[["Badalona","215848","217741","220440","223114","223006"],["Badia del Vallès","13466","13417","13380","13415","13228"],["Barberà del Vallès","32860","32839","33091","33326","33016"],["Barcelona","1620809","1620343","1636762","1663330","1636732"],["Begues","6830","6961","7098","7299","7356"],["Castellbisbal","12297","12332","12390","12538","12610"],["Castelldefels","65954","66375","67004","67449","67226"],["Cerdanyola del Vallès","57723","57740","57403","57845","57217"],["Cervelló","8909","8970","9054","9075","9233"],["Corbera de Llobregat","14439","14643","14822","14876","15017"],["Cornellà de Llobregat","86610","87173","88592","89902","89300"]]}

I supose that the ':' that is referring is the one after {"data":[["Badalona.....

The live edit link is this.
And the admins can see the real datatable debugger in here.

Anyone's help will be useful.:)

Replies

  • kthorngrenkthorngren Posts: 21,322Questions: 26Answers: 4,949

    Checking your JSON with https://jsonlint.com/ is successful. Your test case doesn't replicate the issue. I suspect the Unexpected token ':' error is indicating a syntax error in your Javascript. The console error should point to the line that the unexpected : is on.

    To help debug we will need to see a test case replicating the error. Please post a link to your page or update your test case.

    Kevin

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    You've got a mix of the jQuery and non-jQuery initialisations there:

    $('#form-indicator-table').DataTable('#myTable', {
    

    Should either be:

    $('#form-indicator-table').DataTable({
    

    or

    new DataTable('#form-indicator-table', {
    

    See the manual here for more information.

    Allan

  • mariolcvmariolcv Posts: 3Questions: 0Answers: 0

    I write it wrong on the test case, but in my code was like you said Allan.

    The console refers to the first line in json_data.json

    How can I put the json data manually on the JS BIN live edit.
    (Updated live edit)

    In addition, in my web the table message is 'loading...' istead of 'no data availible in table'.

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    In addition, in my web the table message is 'loading...' istead of 'no data availible in table'.

    That is correct if it hasn't yet loaded the data, which it can't since it is getting an error.

    Can you upload the json_data.json file here so I can take a look at it?

    Allan

  • kthorngrenkthorngren Posts: 21,322Questions: 26Answers: 4,949

    Use the steps in this technote to check the JSON response using https://jsonlint.com/

    How can I put the json data manually on the JS BIN live edit.

    Copy the JSON response use data to load the data instead of ajax. Similar to this example.

    In addition, in my web the table message is 'loading...' istead of 'no data availible in table'.

    Its due to the Javascript errors you are seeing. Script processing has been stopped or Datatables is not seeing the response due to the error.

    Kevin

  • mariolcvmariolcv Posts: 3Questions: 0Answers: 0

    The json example:

    [
        [
            "Badalona",
            "215848",
            "217741",
            "220440",
            "223114",
            "223006"
        ],
        [
            "Badia del Vallès",
            "13466",
            "13417",
            "13380",
            "13415",
            "13228"
        ],
        [
            "Barberà del Vallès",
            "32860",
            "32839",
            "33091",
            "33326",
            "33016"
        ],
        [
            "Barcelona",
            "1620809",
            "1620343",
            "1636762",
            "1663330",
            "1636732"
        ],
        [
            "Begues",
            "6830",
            "6961",
            "7098",
            "7299",
            "7356"
        ]
    ]
    

    when i put this in the data parameter it work well but how can i put an url instead of this static code?

  • kthorngrenkthorngren Posts: 21,322Questions: 26Answers: 4,949

    Are you trying to load the file directly from the filesystem? This isn't allowed by ajax as it is a security issue. You will need a webserver to supply the file.

    Kevin

  • kthorngrenkthorngren Posts: 21,322Questions: 26Answers: 4,949

    After looking at your config it looks like you are using a webserver.

    Have you followed the technote steps to validate the JSON response? What did you find?

    From Allan:

    Can you upload the json_data.json file here so I can take a look at it?

    Kevin

Sign In or Register to comment.