[英]Load HTML table on page load appending with URL response
我已經嘗試在點擊事件中將URL響應放入HTML表中。 但是我真正需要的是,一旦加載頁面,我就必須在表中顯示URL響應。 我的腳本如下:
window.onload = function (
$.ajax({
type: 'GET',
url: 'http://localhost:8080/imp/' ,
dataType:"json", //to parse string into JSON object,
success: function(data){
if(data){
var len = data.length;
var txt = "";
if(len > 0){
for(var i=0;i<len;i++){
if(data[i].name && data[i].age){
txt += "<tr><td>"+data[i].name+"</td><td>"+data[i].age+"</td></tr>";
}
}
if(txt != ""){
$("#impExtTableID").append(txt).removeClass("hidden");
}
}
}
},
error: function(jqXHR, textStatus, errorThrown){
alert('error: ' + textStatus + ': ' + errorThrown);
}
});
);
return false;//suppress natural form submission });
很簡單,我用您的ID制作了一個表格,然后在正文中添加了您從請求URL中獲得的內容,
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
window.onload = function (
$.ajax({
type: 'GET',
url: 'http://localhost:8080/imp/' ,
dataType:"json", //to parse string into JSON object,
success: function(data){
if(data){
var len = data.length;
var txt = "";
if(len > 0){
for(var i=0;i<len;i++){
if(data[i].name && data[i].age){
txt += "<tr><td>"+data[i].name+"</td><td>"+data[i].age+"</td></tr>";
}
}
if(txt != ""){
$("#impExtTableID tbody").html(txt).removeClass("hidden");
}
}
}
},
error: function(jqXHR, textStatus, errorThrown){
alert('error: ' + textStatus + ': ' + errorThrown);
}
});
);
return false;//suppress natural form submission });
</script>
<body>
<table id="impExtTableID" class="hidden">
<thead><th>Name</th><th>Age</th></thead>
<tbody></tbody>
</table>
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.