Tham khảo Demo Ajax blog

Ads By Google

Chúng ta cùng tham khảo một đoạn code Ajax lấy dữ liệu cho blogspot như bên dưới, code ngăn gọn dễ hiểu giúp bạn có thể áp dụng cho việc code blog

Ajax code

$.ajax({
url: ‘http://www.threelas.com/feeds/posts/default?alt=json-in-script&max-results=9&callback=?’,
type: ‘get’,
dataType: “jsonp”,
success: function(data) {
var posturl = “”;
var htmlcode = ‘<ul>’;
for (var i = 0; i < data.feed.entry.length; i++) {
for (var j = 0; j < data.feed.entry[i].link.length; j++) {
if (data.feed.entry[i].link[j].rel == “alternate”) {
posturl = data.feed.entry[i].link[j].href;
break;
}
}
if (“content” in data.feed.entry[i]) {
var postcontent = data.feed.entry[i].content.$t;
} else if (“summary” in data.feed.entry[i]) {
var postcontent = data.feed.entry[i].summary.$t;
} else {
var postcontent = “”;
}
var re = /<S[^>]*>/g;
postcontent = postcontent.replace(re, “”);
//reduce postcontent to 200 characters
if (postcontent.length > 200) {
postcontent = postcontent.substring(0, 200);
}
var posttitle = data.feed.entry[i].title.$t;
htmlcode += ‘<li><div><a href=”‘ + posturl + ‘” target=”_blank”>’ + posttitle + ‘</a></div><div>’ + postcontent + ‘</div></li>’;
}
htmlcode += ‘</ul>’;
document.getElementById(‘result’).innerHTML = htmlcode;
}
});

Ads By Google

ads nè

Đã đóng bình luận.