2016-05-28 12 views
1

jsonデータの内部要素を取得できません。 Here's jsonデータのリンク。ここでURLからjsonデータを取得する方法は?

は私のコードです:注意すべき

$(document).ready(function() { 

    //after button is clicked we download the data 
    $('.button').click(function(){ 

     //start ajax request 
     $.ajax({ 
      url: "http://www.miamia.co.in/dummy/?json=get_recent_posts", 
      //force to handle it as text 
      dataType: "text", 
      success: function(data) { 

       //data downloaded so we call parseJSON function 
       //and pass downloaded data 
       var json = $.parseJSON(data); 
       //now json variable contains data in json format 
       //let's display a few items 
    $('#results').html('Pages:'+ json.pages + '<br />postid: '+json.posts.id);//here I am not able to get the id 
      } 
     }); 
    }); 
}); 
+0

あなたは、Ajaxのエラーが発生したものを見るためにエラーハンドラを追加しようとしたことがありますか?フォームのボタン部分ですか?クリックイベントが発生していますか?多くのトラブルシューティング情報は表示されません。 [ask] – charlietfl

+0

を参照してください。 'dataType:" json "'あなたのajaxでこれを使ってみてください。 – Ramkee

+0

dataTypeをtextではなくjsonにすることはできません。そしてコンソールでjsonを印刷しようとしましたか? – sarath

答えて

0

もの:

  1. dataType: "JSON"
  2. データが返さはJSONですので、JSON
  3. にそれを解析する必要はありませんそれは、data.posts[0].idです〜ではないdata.posts.id

以下のデモが動作します。 console.logまたはブラウザのデベロッパーコンソールで返されたjsonコンテンツを調べることができます。

$(document).ready(function() { 
 

 
    //after button is clicked we download the data 
 
    $('body').on("click", "#button", function() { 
 

 
    //start ajax request 
 
    $.ajax({ 
 
     url: "http://www.miamia.co.in/dummy/?json=get_recent_posts&callback=?", 
 

 
     dataType: "JSON", 
 
     success: function(data) { 
 
     console.log(data); 
 
     //let's display a few items 
 
     $('#results').html('Pages:' + data.pages + '<br />postid: ' + data.posts[0].id); //here I am not able to get the id 
 
     } 
 
    }); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
 
<div id="results"> 
 

 
</div> 
 
<input id="button" type="button" value="Get posts">

関連する問題