【问题标题】:API data not being listed. What am I doing wrong in this code?API 数据未列出。我在这段代码中做错了什么?
【发布时间】:2016-11-27 06:00:39
【问题描述】:

所以我有这个简单的 html 测试页面来测试一些使用 jquery 以 json 格式获取 API 的代码,然后 jquery 通过更新 html 列出数据,但数据没有列出。 API 链接工作正常。数据应该列出数百个返回的数据。我错过了什么。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="../css/screen.css" type="text/css"     media="screen, projection" />
<link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
<!--[if IE]><link rel="stylesheet" href="../css/ie.css" type="text/css" media="screen, projection"><![endif]-->
<link href="../css/highlight.css" rel="stylesheet" type="text/css" media="screen" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
/* <![CDATA[ */
$(document).ready(function(){
$.getJSON("https://coinmap.org/api/v1/venues/",function(data){
    $.each(data.post, function(i,post){
        content += '<p>' + post.id + '</p>';
        content += '<p' + post.name+ '</p>';
        content += '<br/>';
        $(content).appendTo("#posts");
    });
});   
});
/* ]]> */
</script>
</head>
<body>
    <div class="container">
            <div class="span-24">
                   <h2>Check out the following posts:</h2>
                    <div id="posts"></div>
            </div>
    </div>
</body>
</html>

【问题讨论】:

  • 是不是我直接在getJason函数中使用了API链接?我是否需要先通过 PHP 或其他方式获取数据,然后将原始数据文件提供给该函数?

标签: jquery json api


【解决方案1】:

您似乎没有正确访问 API 中的数据。而不是data.post 尝试data.venues

$.each(data.venues, function(i,venue){
    content += '<p>' + venue.id + '</p>';
    content += '<p' + venue.name+ '</p>';
    content += '<br/>';
    $(content).appendTo("#posts");
});

【讨论】:

  • 这确实有效,因为现在我看到页面加载上的数据。不知道为什么它只是通过更改变量名称来工作,但它确实这样做了谢谢:-)
  • 我现在看到场地在 JSON 中列出,因此我们可以先通过它访问数据。我现在明白了
猜你喜欢
  • 2013-01-11
  • 1970-01-01
  • 2015-04-23
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多