0

Hey Im a begginer when it comes to new age html code (i consider json and javascript code new age, because i learned on just straight html code). Well What im doing is trying to get all the data from my .json file and put in a table, but it keeps saying i cannot load resource and it says the .json file.

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" charset="text">       </meta>
  <title> - Jason Onto HTML Displayed</title>

  <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>

<script type="text/javascript">
  $(document).ready(function(){
      $("#DisplyEmp").click(function() {
          $.getJSON("Localhost/Homework_1_second_attempt.json", function(data) {
              for(emp in data.albums) {
                  console.log(data.albums[emp]);
                  var newRow = "<tr>"+
                                  "<td>"+data.albums[emp].title+"</td>"+
                                  "<td>"+data.albums[emp].artist+"</td>"+
                                  "<td>"+data.albums[emp].songs+"</td>"+
                               "</tr>";
                  $("#EmpNewTable").append(newRow);
              } 
          });
      });
  });
</script>


</head>
<body>
<table id="EmpNewTable" border="2"> 
<tr> 
  <th>Artist</th>
  <th>Title</th>
  <th>Songs</th>
</tr> 
</table><br /><br /> 
<input type="button" id="DisplyEmp" value="Display" />

</body></html>

thank you everyone for helping me with my problem

4

2 回答 2

0

Due to security concerns, browsers will not allow you to ever load resources from any domain other than your own. That includes the file:/// "protocol".

To do this, you will need to run a server. From the looks of it, maybe you are running this on http://localhost. Then change your ajax url to

$.getJSON("/Homework_1_second_attempt.json...

The relative path will start at the same location as the file you are viewing.

于 2013-03-28T04:16:27.493 回答
-1

尝试以下操作:

var rows = '';
for(emp in data.albums) {
    rows += "<tr>"+
                "<td>"+emp.title+"</td>"+
                "<td>"+emp.artist+"</td>"+
                "<td>"+emp.songs+"</td>"+
            "</tr>";
}
$("#EmpNewTable").append(rows);
于 2013-03-28T04:22:35.173 回答