cant load json data - Uncaught TypeError: Cannot read property 'length' of undefined

cant load json data - Uncaught TypeError: Cannot read property 'length' of undefined

santokornsantokorn Posts: 4Questions: 1Answers: 0

Hi, I try to load a object.txt file with json objectc data, trhow this error :

Uncaught TypeError: Cannot read property 'length' of undefined

//js file ====================================

$(document).ready(function() {
$('#example').dataTable( {
"ajax": 'js/objetcs.txt',
"columns": [
{ "data": "title" },
{ "data": "id" },
{ "data": "subtitle" },
{ "data": "urlImg" },
{ "data": "urlImgXl" },
{ "data": "detail" },
{ "data": "type" },
{ "data": "info" }
]
} );
} );

HTML file ==================================

title id subtitle urlImg. urlImgXl detail type info
title id subtitle urlImg. urlImgXl detail type info

JSON data ===================================

{
"noticeArray":[
{
"title":"TITULO",
"id":9,
"subtitle":"The JSON Formatter was created",
"urlImg":"img/notice/1.jpg",
"urlImgXl" : "img/notice/xl/5.jpg",
"detail":"The JSON Formatter was created to help with debugging.The JSON Formatter was created to help with,",
"type":"games",
"info" : "The JSON Formatter was created to help with debugging. created to help with debugging.created to help with debugging.created to help with debugging.reated to help with debugging.created to help with debugging.created to help with debugging."
},
{
"title":"TITULO",
"id":6,
"subtitle":"The JSON Formatter was created",
"urlImg":"img/notice/2.jpg",
"urlImgXl" : "img/notice/xl/5.jpg",
"detail":"The JSON Formatter was created to help with debugging.",
"type":"tec",
"info" : "The JSON Formatter was created to help with debugging. created to help with debugging.created to help with debugging.created to help with debugging."
}
]}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    Please follow the forum rules and link to a test case showing the issue.

    Allan

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Your page throws this error:

    "NetworkError: 404 Not Found - http://santiago.loopzer.net/santiago/UOPA/js/objetcs.txt.k?_=1421345010736"

  • santokornsantokorn Posts: 4Questions: 1Answers: 0

    Sorry, I change to force a url error to see if this changed de error msj! now is right! you can see! sorry by my english! ;)

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Answer ✓

    By defailt DataTables expects to find the array of data to use for the table in the object parameter data in the returned JSON. However, you have noticeArray, which is absolutely fine, but DataTables can't know that the data is there, unless you tell it so.

    To tell it to use noticeArray you would use the ajax.dataSrc option.

    Allan

  • santokornsantokorn Posts: 4Questions: 1Answers: 0

    Thank you very much Allan, now load the json file correctly, this is the final script:

    $('#example').dataTable( {
    "ajax": {
    "url": "js/notice.json",
    "dataSrc": "noticeArray"
    },
    "columns": [
    { "data": "title" },
    { "data": "id" },
    { "data": "subtitle" },
    { "data": "urlImg" },
    { "data": "urlImgXl" },
    { "data": "detail" },
    { "data": "type" },
    { "data": "info" }

        ] 
    

    } );

This discussion has been closed.