I am new to JSON.
I was doing a project on HTML, JSON and jQuery recently. Thing I want to achieve now is get data from the JSON file and load it into my table. The data was not loaded into my table.
My json file contact.json
{
"length": 2,
"info": [
{
"name":"Sam",
"email":"fred@server.com",
"phone":"789456235"
},
{
"name":"Fred",
"email":"fred@server.com",
"phone":"125689564"
}
]
}
My script to load data:
window.onload = function () {
var contacts;
setTimeout(function(){ //pass it an anonymous function that calls foo
loadData("contact");
},2000);
};
function loadData(myfile){
$.getJSON( myfile + ".json", function(data){
console.log(data)
$.each(data, function(index, element){
$.each(element, function(i, item){
$('#contacts').append('<tr><td>' + item.name + '</td><td>'+ item.email +'</td><td>' + item.phone + '</td><td>');
});
});
});
}
My HTML
<body>
<div id="tt" class="easyui-tabs" style="width:400px;height:250px;">
<div title="Home">
<table id='contacts'></table>
</div>
</div>
I copied the whole thing from Create contact table from JSON data
This is the error
TypeError: j is undefined
http://code.jquery.com/jquery-1.4.4.min.js
Line 32
I'm getting the object in the console. but the data is not loaded. How do I fix this problem?