javascript - 如何从html中的url获取json数据
问题描述
我正在尝试从以下位置获取 json 数据:https ://www.topurbanaradio.com/ps/scast.php
我尝试使用几个对其他人有用的代码,但我无法从 URL 中获取任何信息。
我尝试的最后一个代码是这个:
$(function() {
var entries = [];
var JSONurl = "https://www.topurbanaradio.com/ps/scast.php";
$.getJSON(JSONurl, function(data) {
$.each(data.entries, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.cancion + "</td>" + "<td>" + f.artista + "</td>" + "<td>" + f.almbumart + "</td>" + "</tr>"
$(tblRow).appendTo("#entrydata tbody");
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<body>
<div class="wrapper">
<div class="profile">
<table id="entrydata" border="1">
<thead>
<th>Artista</th>
<th>Cancion</th>
<th>AlbumArt</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>
解决方案
在tbody中为您的表添加tr并为您从. 然后将结果附加到tr我还建议在结果显示时使用加载器。$each
api
使用CORS 扩展打开它
注意:我使用的是 firefox + CORS
$(function() {
var entries = [];
var JSONurl = "https://www.topurbanaradio.com/ps/scast.php";
$.getJSON(JSONurl, function(data) {
$.each(Object.values(data), function(i, value) {
console.log(value)
var tblRow = "<td>" + value + "</td>"
$(tblRow).appendTo("#entrydata tbody tr");
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<body>
<div class="wrapper">
<div class="profile">
<table id="entrydata" border="1">
<thead>
<th>Artista</th>
<th>Cancion</th>
<th>AlbumArt</th>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
</div>
</div>
</body>
推荐阅读
- ios - IB Designables:无法渲染和更新自动布局状态(谷歌地图)
- windows - 使用 mingw_64 在 Windows 上编译 Rust 时无法加载 codegen 后端
- mongodb - Mongo DB结果接口到Golang中的结构转换
- c# - 在 Mediatr 命令处理程序中注入依赖项
- database - Tomcat 8 中的 AbstractMethodError
- python-3.x - 如何在python中使用for循环读取具有多张工作表的excel文件
- reactjs - 获取自定义属性值
- db2 - 将十进制数转换为时间
- java - 如何使用 Fragment View Pager 创建待办事项列表应用程序
- python-3.x - 循环遍历 json 对象并使用相同的键添加到字典并附加到列表