2017-06-07 2 views
0

Ich versuche, json Daten von Rest Web-Services in HTML-Tabelle zu füllen. dies ist mein HTML-Codebefüllen Json Daten in HTML-Tabelle mit Ajax Anruf

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Employee List</title> 
</head> 
<body> 
    <h1>Employee List</h1> 
    <form> 
     <table id= "content"> 
     <thead> 
      <tr> 
       <th>Id</th> 
       <th>Name</th> 
       <th>Age</th> 
       <th>Salary</th> 
       <th>Address</th> 
      </tr> 
     </thead> 
     <tbody id = "emp"> 
     </tbody>  
     </table> 
    </form> 
    <script> 

    $(document).ready(function(){ 
     alert("1"); 
     var data = $("#employee").val(); 
     alert("2"); 
     $.ajax({ 

      url: '/rest/restemployees', 
      type: 'GET', 
      dataType: 'JSON', 
      success: function(data){ 
       alert("3"); 
       $.each(data, function(i,value){ 
        $("#content tbody").append("<tr>" + "<td>" + value.id + "</td>" + 
          "<td>" + value.name + "</td>" + 
          "<td>" + value.age + "</td>" + 
          "<td>" + value.salary + "</td>" + 
          "<td>" + value.address + "</td>" + "</tr>") 
       }); 

      }, 
      error: function(data){ 
       alert("4"); 
      } 

     }); 
    }); 

    </script> 
</body> 
</html> 

mir bitte

Anmerkung helfen: Warnungen genutzt werden einfach um den Fluss zu wissen

Inhalt der Tabellen-ID ist. Ich weiß nicht, wo der Fehler ist. kann mir jemand helfen, dieses Problem zu lösen

Antwort

0

würde ich so etwas wie

success: function(data){ 
       alert("3"); 
       $(data).each(function(){ 
        $('#tbody').append('<tr><td>' + this.id + '</td><td>' + this.name + '</td><td>' + this.age + '</td><td>' + this.salary + '</td><td>' + this.address + '</td></tr>') 
       }); 

und anstelle des Gebens Tabelle id tun nur eine id Tabellenkörper stellen, wo Sie anhängen

wollen