html - 如何将csv文件转换为html表?
问题描述
<!DOCTYPE html>
<html>
<head>
<title>CSV File to HTML Table Using AJAX jQuery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="table-responsive">
<h1 align="center">CSV File to HTML Table Using AJAX jQuery</h1>
<br />
<div align="center">
<button type="button" name="load_data" id="load_data" class="btn btn-info">Load Data</button>
</div>
<br />
<div id="employee_table">
</div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function () {
$('#load_data').click(function () {
$.ajax({
url: "iguana.csv",
dataType: "text",
success: function (data) {
var employee_data = data.split(/\r?\n|\r/);
var table_data = '<table class="table table-bordered table-striped">';
for (var count = 0; count < employee_data.length; count++) {
var cell_data = employee_data[count].split(",");
table_data += '<tr>';
for (var cell_count = 0; cell_count < cell_data.length; cell_count++) {
if (count === 0) {
table_data += '<th>' + cell_data[cell_count] + '</th>';
}
else {
table_data += '<td>' + cell_data[cell_count] + '</td>';
}
}
table_data += '</tr>';
}
table_data += '</table>';
$('#employee_table').html(table_data);
}
});
});
});
</script>
运行此代码时出现以下错误:
jquery.min.js:4 从源“null”访问“file:///C:/Users/rkuchhadia/Music/Dash/database_backup/iguana.csv”处的 XMLHttpRequest 已被 CORS 策略阻止:跨源请求是仅支持协议方案:http、data、chrome、chrome-extension、https。
解决方案
推荐阅读
- mysql - 注册新帐户 MySql
- r - Using apply function to do claculation in data.frame without change the format of my date column
- apache-spark - object has no attribute 'map' error in pyspark 2.4.4
- java - How to Delete files from the directory by specifying it in the output application
- python-3.x - sys.path 中的 /usr/lib/python37.zip
- c++ - 最大子数组和和那些索引-分而治之
- google-cloud-storage - 如何将文件从 Google Cloud Storage 导入在 R 中运行的 H2O
- asp.net-core - ASP.NET Core WebAPI 中的 POST 服务
- google-chrome - 从移动设备打开已安装的 Web 应用程序时出现问题(添加到主屏幕)
- c++ - 在 Windows 中通过 C/C++ 从 BITMAP[+WAVE(s)] 到 RTSP (Twitch) 的路径是什么?