cant load json data - Uncaught TypeError: Cannot read property 'length' of undefined
cant load json data - Uncaught TypeError: Cannot read property 'length' of undefined
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
Please follow the forum rules and link to a test case showing the issue.
Allan
http://santiago.loopzer.net/santiago/UOPA/grilla.html
Your page throws this error:
"NetworkError: 404 Not Found - http://santiago.loopzer.net/santiago/UOPA/js/objetcs.txt.k?_=1421345010736"
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! ;)
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 havenoticeArray
, 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 theajax.dataSrc
option.Allan
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" }
} );