javascript - 尝试从json获取数据

标签 javascript html json web

我将尝试从 url json 获取数据,但不附加任何内容 这是我的代码非常简单:

    <html>
    <head>
    <title>The jQuery Example</title>
    <script type = "text/javascript" 
    src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type = "text/javascript" language = "javascript">
                 $(document).ready(function() {
                    $("#driver").click(function(event){
                       $.getJSON('http://api.walmartlabs.com/v1/items/54732749?format=json&apiKey=fc5cku3vruymkhxhvtenm9bk', function(jd) {
                          $('#stage').html('<p> Name: ' + jd.item.name + '</p>');
                          $('#stage').append('<p>Age : ' + jd.item.itemId+ '</p>');
                          $('#stage').append('<p> Sex: ' + jd.item.salePrice+ '</p>');
                       });
                    });
                 });
              </script>
           </head>
           <body>
              <p>Click on the button to load result.html file:</p>
              <div id = "stage" style = "background-color:#cc0;">
                 STAGE
              </div>
              <input type = "button" id = "driver" value = "Load Data" />
   </body>
   </html>

有人可以知道为什么我什么也看不到吗?

最佳答案

您的数据未包含在您需要直接访问的item对象中。

<script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#driver").click(function(event){
               $.getJSON('http://api.walmartlabs.com/v1/items/54732749?format=json&apiKey=fc5cku3vruymkhxhvtenm9bk', function(jd) {
                  $('#stage').html('<p> Name: ' + jd.name + '</p>');
                  $('#stage').append('<p>Age : ' + jd.itemId+ '</p>');
                  $('#stage').append('<p> Sex: ' + jd.salePrice+ '</p>');
               });
            });
         });
      </script>

关于javascript - 尝试从json获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40648633/

相关文章:

javascript - 为什么我的行设置为可见后不可见?

html - Div,只是我需要的一个小例子

asp.net - 如何发送post请求?

java - 来自 2 个与用户位置配合使用的 AsyncTask,只有一个适用于 Java 和 Android

python - 如何使用 wreq 在 haskell 中发出 json 请求?

javascript - 单击按钮不单击到表行

javascript - 如何比较 JSON 对象和 Waterline 模型实例?

javascript - 修改生成器函数原型(prototype)

html - Bootstrap 中的锯齿形布局

jquery - 在 mixitup jquery 中为图像添加字幕