我发誓这是昨晚工作的,这就是为什么我能够在凌晨1点睡觉;)。我今天早上上班,现在它永远不会受到FB.api的回调。 FB.api是一个异步的Facebook JS电话......但不管昨晚工作正常,我认为这不是facebook服务器(终点)问题。
function BindAlbumAndPhotoData()
{
GetAllAlbums(userID, accessToken, function(aAlbums)
{
alert("we're back and should have data");
if (aAlbums === null || aAlbums === undefined) {
alert("array is empty");
return false;
}
var defaultAlbumID = aAlbums[0].id;
alert(" defaultAlbumID: " + defaultAlbumID);
});
};
function GetAllAlbums(userID, accessToken, callbackFunctionSuccess)
{
var aAlbums = []; // array
var uri = "/" + userID + "/albums?access_token=" + accessToken;
alert("uri: " + uri);
FB.api(uri, function (response)
{
alert("inside FB.api");
// check for a valid response
if (!response || response.error)
{
alert("error occured");
return;
}
for (var i = 0, l = response.data.length; i < l; i++) {
alert("Album #: " + i + "\r\n" +
"response.data[i].id: " + response.data[i].id + "\r\n" +
"response.data[i].name: " + response.data[i].name + "\r\n" +
"response.data[i].count: " + response.data[i].count + "\r\n" +
"response.data[i].link: " + response.data[i].link
);
aAlbums[i] = new Album(
response.data[i].id,
response.data[i].name,
response.data[i].count,
response.data[i].link
);
alert("aAlbums[" + i + "].id : " + aAlbums[i].id);
}
alert("about to pass back the array to the callback function");
callbackFunctionSuccess(aAlbums);
});
}
所以它得到了uri的警报,但从来没有点击FB.api回调中的第一个警报。意味着它永远不会发出警报(“FB.api内部”);我不知道为什么。
已更新
看起来我所有的api电话都被中止了。不知道什么会中止它(FB服务器?):
alt text http://elbalazo.net/post/abortedcalls.jpg
更新#2
好看,中止只是说Http请求已经完成。如果我右键单击Firebug中的中止行并选择“在新选项卡中打开”,它会显示HttpResponse数据...但我认为这是因为它在我执行此操作时发送手动请求,就像您复制并粘贴此请求网址一样手动进入浏览器......
另一方面,如果我右键单击该中止行并选择“复制响应标题”,我什么也得不到。另外,当我在Chrome中查看此内容时,我会看到请求标头,但看不到任何响应标头。
所以,如果我从FB.api调用中获取数据,那么为什么它不会进入回调呢?
更新#3
好的,让这个再次运作。我在FB.api的回调中调用了回调方法。所以这是轰炸。
function BindFacebookAlbumAndPhotoData()
{
GetAllFacebookAlbums(userID, accessToken, function(aAlbums)
{
if (aAlbums === null || aAlbums === undefined) {
alert("array is empty");
return false;
}
// Set the default albumID
var defaultAlbumID = aAlbums[0].id;
};
函数GetAllAlbums(userID,accessToken,callbackFunctionSuccess) { var aAlbums = []; //数组 var uri =“/”+ userID +“/ albums?access_token =”+ accessToken;
//alert("uri: " + uri);
FB.api(uri, function (response) {
alert("inside FB.api");
// check for a valid response
if (!response || response.error) {
alert("error occured");
return;
}
for (var i = 0, l = response.data.length; i < l; i++) {
alert("Album #: " + i + "\r\n" +
"response.data[i].id: " + response.data[i].id + "\r\n" +
"response.data[i].name: " + response.data[i].name + "\r\n" +
"response.data[i].count: " + response.data[i].count + "\r\n" +
"response.data[i].link: " + response.data[i].link
);
aAlbums[i] = new FacebookAlbum(
response.data[i].id,
response.data[i].name,
response.data[i].count,
response.data[i].link
);
alert("aAlbums[" + i + "].id : " + aAlbums[i].id);
}
});
alert("about to pass back the array to the callback function");
callbackFunctionSuccess(aAlbums);
}
答案 0 :(得分:1)
嗯尝试以下
function GetAllAlbums(userID, accessToken, callbackFunctionSuccess)
{
var aAlbums = []; // array
var uri = "/" + userID + "/albums?access_token=" + accessToken;
alert("uri: " + uri);
FB.api(uri,GetAllAlbumsCallback);
}
function GetAllAlbumsCallback(Data)
{
alert("inside FB.api");
// check for a valid response
if (!Data || Data.error)
{
return;
}
for (var i = 0, l = Data.data.length; i < l; i++)
{
aAlbums[i] = new Album(
Data.data[i].id,
Data.data[i].name,
Data.data[i].count,
Data.data[i].link
);
if (aAlbums === null || aAlbums === undefined)
{
alert("array is empty");
return false;
}
var defaultAlbumID = aAlbums[0].id;
alert(" defaultAlbumID: " + defaultAlbumID);
}
}
尝试将回调与匿名分开,有时可能存在范围问题:/
将它们分开应该给它们一个全局范围,并且通常不易出错。