jquery - how to fetch json data from url? -
i not able fetch inside elements of json data. here's link json data.
here's code:
$(document).ready(function() { //after button clicked download data $('.button').click(function(){ //start ajax request $.ajax({ url: "http://www.miamia.co.in/dummy/?json=get_recent_posts", //force handle text datatype: "text", success: function(data) { //data downloaded call parsejson function //and pass downloaded data var json = $.parsejson(data); //now json variable contains data in json format //let's display few items $('#results').html('pages:'+ json.pages + '<br />postid: '+json.posts.id);//here not able id } }); }); });
things note:
datatype: "json"
- data returned json, there no need parse json
- it
data.posts[0].id
, notdata.posts.id
the following demo works. can examine json content returned in console.log
or in developer console of browser.
$(document).ready(function() { //after button clicked download 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 few items $('#results').html('pages:' + data.pages + '<br />postid: ' + data.posts[0].id); //here not able 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">
Comments
Post a Comment