I want to import a json file when i call $(document).ready(function (){}
. That properties defined in the json file are needed for my html form.
How can i do that?
Asked
Active
Viewed 245 times
0

Ratha
- 9,434
- 17
- 85
- 163
-
1try this `$.getJSON('path', function(data){ console.log(data); })` – Girish Sep 19 '14 at 11:42
2 Answers
2
try like this:
$.getJSON( "path to json file", function( data ) {
var item = [];
$.each( data, function( key, val ) {
item.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "myclass",
html: item.join( "" )
}).appendTo( "body" );
});

Suchit kumar
- 11,809
- 3
- 22
- 44
1
An example how to do this could be:
<script type="text/javascript">
$(function(){
$.getJSON('names.json',function(data){
console.log('success');
$.each(data.employees,function(i,emp){
$('ul').append('<li>'+emp.firstName+' '+emp.lastName+'</li>');
});
}).error(function(){
console.log('error');
});
});
</script>

munsifali
- 1,732
- 2
- 24
- 43