javascript - can't fetch data from json with jquery ajax -
i started learning json using jquery , i'm having difficulties, i've spent 2 days, i've followed lots of tutorial both on youtube , blogs show isn't have in case. json different mine. got multiple keys i've 1 "news"
the json response server this:
{ "news":[ { "id":48927, "name": "the title goes right here", "url": "www.example.com", "date":"16 august 2013", "image":"img.example.com\/image.jpg" }, { "id": 48908, "name":"the title goes right here — photography", "url":"www.example.net", "date":"17 august 2013", "image":"img.example.net\/image2.jpg" } ] }
in reality server more this:
{"news":[{"id":48927,"name": "the title goes right here","url": "www.example.com","date":"16 august 2013","image":"img.example.com\/image.jpg"},{"id": 48908,"name":"the title goes right here — photography","url":"www.example.net","date":"17 august 2013","image":"img.example.net\/image2.jpg"}]}
but both valid! i've check json lint.
so i'm not able info json , append them in #posts
i've tried both $.getjson & $.ajax undefined response
$(document).ready(function() { $.ajax({ url: 'http://s.example.com/myjson.php', data: 'get', datatype: 'json', success: function(data){ $('#posts').append('name: ' + data.news.id + '<br/>'); }, }); });
in code above i've tried in append data, data.id, data.news, data.url etc. alway getting undefined or null.
$(document).ready(function() { $.getjson("http://s.example.com/myjson.php", function(data) { $.each(data, function(key, value) { $("#posts").append("<li>"+data.news+"</li>"); }); }); });
same $.ajax tried different things in append nothing.
what want have data id, name, url, date, image appended of "things" i've in json file. unfortunately i'm stuck @ beginning. it's frustrating, because seems can't figure out way it. please me!!
data.news
array have loop through values
$(document).ready(function() { $.getjson("http://s.example.com/myjson.php", function(data) { $.each(data.news, function(key, value) { $("#posts").append("<li>"+value.id+"</li>"); }); }); });
Comments
Post a Comment