javascript - Unable to load data from local json file using jQuery -
<!doctype html> <html> <head> <script data-require="jquery@*" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script> <link rel="stylesheet" href="style.css" /> <script type="text/javascript"> $select = $('#people'); //request json data , parse select element $.ajax({ type:'get', url: 'simple.json', datatype:'json', success:function(data){ //clear current content of select $select.html(''); //iterate on data , append select option $.each(data.person, function(key, val){ $select.append('<option >' + val.firstname+ '</option>'); }) }, error:function(){ //if there error append 'none available' option $select.html('<option >none available</option>'); } }); </script> </head> <body> <select id="people"></select> </body> </html>
the data local json file cannot read using jquery ajax.
even trying same code using $.getjson()
. output not being returned.
this plunker: https://plnkr.co/edit/ys2xxon1oucenl3wqcvj?p=preview
change data.person
data.employees
, wrap code in document ready statement
Comments
Post a Comment