reading json data from a file
I am trying to read a JSON file from a file named sites.js which seems pretty straight forward but I can't get past an error event firing. Initially I did not tell the ajax() call the datatype, and that caused the data to get written to the console as an unrecognized expression.
sites.js as exported from mySQL:
- [
- {
- 'Database' : 'datasite',
- 'Table' : 'sites',
- 'IP' : '192.168.0.101',
- 'Server' : 'ria'
- },
- {
- 'Database' : 'datasite',
- 'Table' : 'columns',
- 'IP' : '192.168.0.101',
- 'Server' : 'ria'
- }
- ]
code to read and display:
- var manageSites = {
- data: [],
- getSites: function(){
- $.ajax({
- type : 'get',
- dataType: 'json',
- url : 'sites.js',
- cache : false,
- error : function(){$('#manage-sites').html("Sites not found")},
- success : function(data){
- manageSites.data = $(data);
- manageSites.showSites();
- }//end success function
- });
- },
-
- showSites : function(){
- var key = '';
- var htm = '<table><thead><tr>';
- for(i=0; i< 1; i++){
- for (key in manageSites.data[0]){
- htm +='<th style="font-size:10pt;">' + key + '</th>'
- }
- htm += '<th style="font-size:10pt;">Remove</th></tr></thead><tbody>'
-
- };
- for(i=0; i<data.length; i++){
- htm +='<tr>';
- htm +='<td>' + manageSites.data[i].Database + '</td>';
- htm +='<td>' + manageSites.data[i].Table + '</td>';
- htm +='<td>' + manageSites.data[i].IP + '</td>';
- htm +='<td>' + maangeSites.data[i].Server + '</td>';
- htm +='<td style="text-align:center;"><input type="checkbox"></td>';
- htm +='</tr>';
-
- };
- htm +='</tbody></table>';
- $('#manage-sites').html(htm);
-
-
- }
-
- }
-
When I execute this after adding the dataType I get "Error sites not found", which is what is supposed to happen when something doesn't work. In my mind I was thinking this would fire if the file couldn't be found, but that's not what is triggering the error to fire here. Any insight is appreciated.