【发布时间】:2015-10-08 04:05:30
【问题描述】:
我正在尝试将 upload.txt 记录显示到 html 表中。
upload.txt 文件位于此网址:http://localhost:3000/upload。当我们调用这个 url 时,它会得到 upload.txt 文件。
其实我在这里使用的是node.js。所以我在这里路由是这样的:
app.get('/upload', function(req, res) {
res.sendfile('views/upload.txt');
});
我的上传文件:
client_ip
1.0.230.145
1.1.145.219
1.1.214.239
1.11.112.100
1.112.218.165
1.112.98.44
1.113.55.77
1.114.193.160
1.115.77.221
1.115.81.190
1.124.150.22
1.124.158.81
我的table.html:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color:#FFBD0C;
}
table {
border-collapse:separate;
border-spacing:1px;
background:#CCC;
margin-right:200px
}
table th {
background:#EEE;
font-weight:600;
padding:10px 20px;
text-align:center;
}
table tbody {
padding:0; margin:0;
border-collapse:collapse;
border-spacing:0px;
}
table td {
background:#C7DBED;
padding:5px 10px;
text-align:center;
}
</style>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js'></script>
<script>
var userListData = [];
$(document).ready(function() {
populateTable();
});
function populateTable() {
var tableContent = '';
$.get( 'http://localhost:3000/upload', function( data ) {
alert(data); // Here I'm able to get the entire text file data.
$.each(data, function(){
alert(data); // here I'm unable to get the data.
tableContent += '<tr>';
tableContent += '<td>' + this.client_ip + '</td>';
tableContent += '</tr>';
});
$('#tablediv').html(tableContent);
});
};
</script>
</head>
<body>
<h2 style="color:brown; margin-left:490px;margin-top:150px;">Upload Clients</h2>
<table width="60%" id="tab" style="margin-left:200px;">
<thead>
<tr>
<th>Clients</th>
</tr>
<thead>
<tbody id="tablediv">
</tbody>
</table>
</body>
</html>
此时,我们如何将client_ip值加载到表中。
function populateTable() {
var tableContent = '';
$.get( 'http://localhost:3000/upload', function( data ) {
alert(data);
$.each(data, function(){
tableContent += '<tr>';
tableContent += '<td>' + this.client_ip + '</td>';
tableContent += '</tr>';
});
$('#tablediv').html(tableContent);
});
};
实际上我无法将数据加载到 html 表中。谁能帮我解决这个问题。
【问题讨论】:
-
本地主机链接无法在线使用...请勿在此处发布!
-
您的网址在
$.get中显示http://localhost:3000/upload。您不应该指定完整的url并带有$.get('http://localhost:3000/upload.txt'...之类的扩展名吗? -
实际上我在我的代码中使用 node.js。我想说的是,如果假设文本文件在服务器中的一个 url,那么如何将文本文件数据加载到html 表格。
-
如果您的上传目录是
http://localhost:3000/upload,您想从脚本中的http://localhost:3000/uploadcsv读取什么 -
您的
$.each将无法正常工作。一旦您收到data作为响应,请尝试在控制台中使用console.log(data)进行日志记录,看看它是如何获得的