Tzahi
Tzahi

Reputation: 268

try to get data fro json

I will try to get data from url json but nothing append this is my code vere simple:

    <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>

some can know why I don't see nothing?

Upvotes: 0

Views: 72

Answers (2)

GiuServ
GiuServ

Reputation: 1235

The problem is with a cross domain Request. You could make an ajax with dataType: "jsonp" to solve your issue.

Look here for better explanation.

note

as already pointed out, you should access to your response data without the .item, so jd.item.name become jd.name

$(document).ready(function() {
  $("#driver").click(function(event) {
    $.ajax({
      url: 'http://api.walmartlabs.com/v1/items/54732749',
      data: {
        format: 'json',
        apiKey: 'fc5cku3vruymkhxhvtenm9bk'
      },
      dataType: 'jsonp',
      success: function(jd) {
        console.log(jd);
        $('#stage').html('<p> Name: ' + jd.name + '</p>');
        $('#stage').append('<p>Age : ' + jd.itemId + '</p>');
        $('#stage').append('<p> Sex: ' + jd.salePrice + '</p>');
      }
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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" />

Upvotes: 0

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14604

Your data is not contained in item object you need to access it directly.

<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>

Upvotes: 2

Related Questions