我有一個JavaScript函數用於調用Facebook API並獲取來自牆上的帖子列表。在Firefox,Chrome和Safari這個沒有問題,但在互聯網 資源管理器 9(我還沒有測試下面),它只是沒有做任何事情,UI保持阻止,但我似乎並沒有得到任何問題指向錯誤的消息。我有一種感覺,它可能與正在返回的JSON和互聯網 Explorer解析器有關,但在這一點上我無法確定。Facebook Graph Call在Internet Explorer 9中不起作用
在被重新指向Facebook SDK之後,我重新實現了這個功能(我不知道爲什麼現在我要離開它),並且它現在似乎都可以很好地與Internet Explorer配合使用。
舊代碼
var getFeed = function (name, type, limit, accessToken, apiKey, containerId) {
var list_url = "https://graph.facebook.com/" + name + "/" + type + "?limit=" + limit + "&access_token=" + accessToken + "&api_key=" + apiKey;
var html = "";
displayHelper.blockUI(containerId, "Loading Feed");
$.getJSON(list_url, function (json) {
//Loop through and within the data array to retrieve the variables.
$.each(json.data, function (i, fb) {
var msg = (typeof (fb.message) != "undefined") ? fb.message : "";
var link = (typeof (fb.link) != "undefined") ? fb.link : "";
var pic = "";
// msg = (typeof(fb.story) != "undefined") ? fb.story : msg;
var type = (typeof (fb.type) != "undefined") ? fb.type : "";
var includeInOutput = true;
pic = getPicture(fb.from.id);
switch (type) {
case "story":
msg = fb.story;
break;
case "link":
if (typeof (fb.message) != "undefined")
msg = fb.message;
else if (typeof (fb.caption) != "undefined")
msg = fb.caption;
else if (typeof (fb.story) != "undefined")
msg = fb.story;
else if (typeof (fb.name) != "undefined")
msg = fb.name;
break;
case "video":
case "photo":
if (typeof (fb.message) != "undefined")
msg = fb.message;
else if (typeof (fb.story) != "undefined")
msg = fb.story;
break;
case "status":
if (typeof (fb.message) != "undefined")
msg = fb.message;
else if (typeof (fb.story) != "undefined")
msg = fb.story;
break;
default:
includeInOutput = false;
break;
}
if (includeInOutput) {
//build html for this list item
html += '<dl class="fb-item">';
html += "<dt>" + fb.from.name + "</dt>";
html += (pic != '') ? '<dd class="img"><img src="' + pic + '" />' : '';
html += '<dd class="msg">' + msg + '</dd>';
/*html += '<a href="' + link + '" class="fb_link" target="_blank">'
+ msg + "(" + type + ")"
+ "</a>";*/
html += '<dd class="links">';
html += '<span>' + fuzzyTime(fb.created_time.replace(/-/g, '/')) + '</span>';
if (typeof (fb.actions) != "undefined") {
if (fb.actions[1].name == "Like")
html += "<a href='" + fb.actions[1].link + "' class='fb_link' target='_blank'>Like</a> - ";
if (fb.actions[0].name == "Comment")
html += "<a href='" + fb.actions[0].link + "' class='fb_link' target='_blank'>Comment</a>";
}
html += '</dd>';
html += "</dl>";
}
}); /* end .each */
//html += "</ul>";
$(containerId).html(html);
$(containerId).unblock();
}); /* end getJSON */
} /* end hetFeed
新代碼 - 再次更新。圖片沒有返回,因此我提取了消息並將其構建到自己的方法中,並在獲取圖片的回調中構建了消息。在我以阻塞的方式進行之前,錯了!希望這有助於某一天。
var postMessage = function (fb, containerId) {
FB.api("/" + fb.from.id + "/?fields=picture", {}, function (p) {
var pic = p.picture;
var msg = (typeof (fb.message) != "undefined") ? fb.message : "";
var link = (typeof (fb.link) != "undefined") ? fb.link : "";
var type = (typeof (fb.type) != "undefined") ? fb.type : "";
var includeInOutput = true;
var html = "";
switch (type) {
case "story":
msg = fb.story;
break;
case "link":
if (typeof (fb.message) != "undefined")
msg = fb.message;
else if (typeof (fb.caption) != "undefined")
msg = fb.caption;
else if (typeof (fb.story) != "undefined")
msg = fb.story;
else if (typeof (fb.name) != "undefined")
msg = fb.name;
break;
case "video":
case "photo":
if (typeof (fb.message) != "undefined")
msg = fb.message;
else if (typeof (fb.story) != "undefined")
msg = fb.story;
break;
case "status":
if (typeof (fb.message) != "undefined")
msg = fb.message;
else if (typeof (fb.story) != "undefined")
msg = fb.story;
break;
default:
includeInOutput = false;
break;
}
if (includeInOutput) {
//build html for this list item
html += '<dl class="fb-item">';
html += "<dt>" + fb.from.name + "</dt>";
html += (pic != '') ? '<dd class="img"><img src="' + pic + '" />' : '';
html += '<dd class="msg">' + msg + '</dd>';
/*html += '<a href="' + link + '" class="fb_link" target="_blank">'
+ msg + "(" + type + ")"
+ "</a>";*/
html += '<dd class="links">';
html += '<span>' + fuzzyTime(fb.created_time.replace(/-/g, '/')) + '</span>';
if (typeof (fb.actions) != "undefined") {
if (fb.actions[1].name == "Like")
html += "<a href='" + fb.actions[1].link + "' class='fb_link' target='_blank'>Like</a> - ";
if (fb.actions[0].name == "Comment")
html += "<a href='" + fb.actions[0].link + "' class='fb_link' target='_blank'>Comment</a>";
}
html += '</dd>';
html += "</dl>";
}
$(containerId).append(html);
});
}
var getFeed = function (name, type, limit, accessToken, apiKey, containerId) {
var list_url = "https://graph.facebook.com/" + name + "/" + type + "?limit=" + limit + "&access_token=" + accessToken + "&api_key=" + apiKey;
var fullHtml = "";
helper.blockUI(containerId, "Loading Feed");
var path = "/" + name + "/" + type;
FB.api(path, { access_token: accessToken, api_key: apiKey, limit: limit }, function (json) {
console.log(json);
var data = json.data;
for (var i = 0, l = data.length; i < l; i++) {
var fb = data[i];
postMessage(fb, containerId);
} //End For
$(containerId).unblock();
});
} /* End getFeed */
不回答你的問題,但我很好奇你爲什麼不使用[facebook javascript sdk](http://developers.facebook.com/docs/reference/javascript/)。它會照顧添加所需的參數併爲您發出ajax調用,因爲您沒有使用它? – 2012-03-06 20:02:44
老實說,我一直在努力的API和發現類似的代碼只是爲了讓它移動。 – Modika 2012-03-07 06:13:29