0

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?

Raghbendra Nayak
  • 1,606
  • 3
  • 22
  • 47
Tzahi
  • 259
  • 3
  • 12
  • its possible that your URL is wrong OR the click event is not firing (try jquery on (http://api.jquery.com/on/) – Omasu Plus Nov 17 '16 at 07:07

2 Answers2

2

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>
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
  • True! @Leopard. Tzahi you are having directly object not and key so you can access directly the fields of the object as jd.name, jd.itemId etc. – PassionInfinite Nov 17 '16 at 07:12
  • @Tzahi please check your console you may be getting an error. May be your jquery file hasn't loaded. – Mairaj Ahmad Nov 17 '16 at 07:19
  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://api.walmartlabs.com/v1/items/54732749?format=json&apiKey=fc5cku3vruymkhxhvtenm9bk. (Reason: CORS header 'Access-Control-Allow-Origin' missing). – Tzahi Nov 17 '16 at 07:21
  • So this isn't your own api ? You need to have access to this api. – Mairaj Ahmad Nov 17 '16 at 07:22
  • Have a look [here](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) – GiuServ Nov 17 '16 at 07:25
0

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" />
Community
  • 1
  • 1
GiuServ
  • 1,215
  • 1
  • 13
  • 33