我对此网址有疑问http://surfujpametno.roditelji.me/category/tv-emisija/?json=get_all_posts 我收到此错误: 未捕获的TypeError:无法读取未定义的属性“url”
但我对此网址http://surfujpametno.roditelji.me/category/vijesti/?json=get_all_posts
没有问题这是我的代码:
function json_parser_fun() {
var url = 'http://surfujpametno.roditelji.me/category/tv-emisija/?json=get_all_posts';
$.ajax({
url: url,
type: 'GET',
dataType: 'jsonp',
success: function(json){
console.debug(json);
var JSONstring = JSON.stringify(json);
var myData = JSON.parse(JSONstring);
var postovi = "";
var brojac = 1;
var ima = 0;
for(var i=0;i<myData.posts.length;i++)
{
if(brojac %2 ==0)
postovi = postovi + "<a onClick=moja_funkcija('"+ myData.posts[i].url +"') class='menu_item' href='#'><div class='menu_div"+2+"'><div class='image_div'><img style='height:40px;width:40px;' src='"+ myData.posts[i].attachments[0].url +"'/></div><div class='title_div'><p style='font-size:9px'>" + myData.posts[i].date + "</p><p style='font-size:12px'>" + myData.posts[i].title + "</p></div></div></a>";
else
postovi = postovi + "<a onClick=moja_funkcija('"+ myData.posts[i].url +"') class='menu_item' href='#'><div class='menu_div'><div class='image_div'><img style='height:40px;width:40px;' src='"+ myData.posts[i].attachments[0].url +"'/></div><div class='title_div'><p style='font-size:9px'>" + myData.posts[i].date + "</p><p style='font-size:12px'>" + myData.posts[i].title + "</p></div></div></a>";
brojac++;
}
document.getElementById("container").innerHTML = postovi;
},
error: function(e) {
console.log(e.message);
}
});
}
答案 0 :(得分:0)
在JSON响应中,有时附件是空的。
示例#1:空附件替换为urlNo_attachments_url
function json_parser_fun() {
var url = 'http://surfujpametno.roditelji.me/category/tv-emisija/?json=get_all_posts';
var urlNo_attachments_url = 'http://animatika.ru/netcat_files/userfiles/3/facepalm.jpg';
$.ajax({
url: url,
type: 'GET',
dataType: 'jsonp',
success: function(json){
console.debug(json);
var JSONstring = JSON.stringify(json);
var myData = JSON.parse(JSONstring);
var postovi = "";
var brojac = 1;
var ima = 0;
for(var i=0;i<myData.posts.length;i++)
{
if(brojac %2 ==0)
postovi = postovi + "<a onClick=moja_funkcija('"+ myData.posts[i].url +"') class='menu_item' href='#'><div class='menu_div"+2+"'><div class='image_div'><img style='height:40px;width:40px;' src='"+ (myData.posts[i].attachments && myData.posts[i].attachments[0]?myData.posts[i].attachments[0].url:urlNo_attachments_url) +"'/></div><div class='title_div'><p style='font-size:9px'>" + myData.posts[i].date + "</p><p style='font-size:12px'>" + myData.posts[i].title + "</p></div></div></a>";
else
postovi = postovi + "<a onClick=moja_funkcija('"+ myData.posts[i].url +"') class='menu_item' href='#'><div class='menu_div'><div class='image_div'><img style='height:40px;width:40px;' src='"+ (myData.posts[i].attachments && myData.posts[i].attachments[0]?myData.posts[i].attachments[0].url:urlNo_attachments_url) +"'/></div><div class='title_div'><p style='font-size:9px'>" + myData.posts[i].date + "</p><p style='font-size:12px'>" + myData.posts[i].title + "</p></div></div></a>";
brojac++;
}
document.getElementById("container").innerHTML = postovi;
},
error: function(e) {
console.log(e.message);
}
});
}
示例#2:如果是空附件 - 发布删除
function json_parser_fun() {
var url = 'http://surfujpametno.roditelji.me/category/tv-emisija/?json=get_all_posts';
$.ajax({
url: url,
type: 'GET',
dataType: 'jsonp',
success: function(json){
console.debug(json);
var JSONstring = JSON.stringify(json);
var myData = JSON.parse(JSONstring);
var postovi = "";
var brojac = 1;
var ima = 0;
for(var i=0;i<myData.posts.length;i++)
{
if(myData.posts[i].attachments && myData.posts[i].attachments[0]){
if(brojac %2 ==0)
postovi = postovi + "<a onClick=moja_funkcija('"+ myData.posts[i].url +"') class='menu_item' href='#'><div class='menu_div"+2+"'><div class='image_div'><img style='height:40px;width:40px;' src='"+ myData.posts[i].attachments[0].url +"'/></div><div class='title_div'><p style='font-size:9px'>" + myData.posts[i].date + "</p><p style='font-size:12px'>" + myData.posts[i].title + "</p></div></div></a>";
else
postovi = postovi + "<a onClick=moja_funkcija('"+ myData.posts[i].url +"') class='menu_item' href='#'><div class='menu_div'><div class='image_div'><img style='height:40px;width:40px;' src='"+ myData.posts[i].attachments[0].url +"'/></div><div class='title_div'><p style='font-size:9px'>" + myData.posts[i].date + "</p><p style='font-size:12px'>" + myData.posts[i].title + "</p></div></div></a>";
brojac++;
}
}
document.getElementById("container").innerHTML = postovi;
},
error: function(e) {
console.log(e.message);
}
});
}