我有四個函數來處理我的應用程序的某個部分獲取照片url。Javascript函數返回undefined
在函數handleGetPhotoResponse中,alert中有我的url,它看起來應該是一切。
問題出在函數handleGetUsersFurKidsResponse。變量「fkimg」未定義。誰能告訴我我錯了哪裏?
要使用這部分的應用程序,我打電話給「getUsersFurKids」。
function handleGetPhotoResponse(responseText, size) {
var photoDetails = JSON.parse(responseText);
var thePhoto = photoDetails[size];
alert(thePhoto);
return thePhoto;
}
function getPhoto(id, size) {
var url = "url-removed"+id;
var request = new XMLHttpRequest();
//Send the proper header information along with the request
request.open("GET", url);
request.onload = function() {
if (request.status == 200) {
return handleGetPhotoResponse(request.responseText, size);
}
};
request.send(null);
}
// function to handle the response of getting a users fur kids
function handleGetUsersFurKidsResponse(responseText) {
var ul = document.getElementById("furKidList");
var furKids = JSON.parse(responseText);
for(var i = 0; i<furKids.length; i++){
var li = document.createElement("li");
var fkimg = getPhoto(furKids[i].ui_id, 'small');
li.innerHTML = "<a href=\"\"><img src=\""+fkimg+"\"> "+furKids[i].p_name;
ul.appendChild(li);
}
}
// function to get a users fur kids
function getUsersFurKids(id) {
// api url for getting fur kids
var url = "url-removed"+id;
var request = new XMLHttpRequest();
//Send the proper header information along with the request
request.open("GET", url);
request.onload = function() {
if (request.status == 200) {
handleGetUsersFurKidsResponse(request.responseText);
}
};
request.send(null);
}
那麼確定。 'getPhoto()'不返回任何東西。 –
我以爲「return handleGetPhotoResponse(request.responseText,size);」會返回價值? –
@ClintChaney:它返回函數'request.onload()'內的值,而不是'getPhoto()'。此外,被調用的函數和響應GET請求的服務器之間存在延遲。 – JCOC611