2013-11-03 21 views
1

附加圖像scr +變量的語法是什麼。jquery ajax在一個變量中添加圖像src attr並在追加之前等待服務器響應

$('#FBProfileImgVar').attr('src', 'https://graph.facebook.com/' + response.id + '/picture?type=large'); 

上面顯示「未定義」在一個地方,它的假設是一個變量

編輯1.也許問題在於阿賈克斯,如何使上面的代碼行要等到響應在追加之前是否存在?

+2

假設response.id存在,並且設置,那麼你的代碼如下好吧 – mplungjan

+1

它看起來很好,我對你的ajax調用和結果感興趣 – 2013-11-03 14:42:39

+1

這段代碼看起來很完美蓋。這個問題很可能在於另一段代碼。也許'response'變量不包含你期望的內容。 – Lix

回答

0

而不是使用response.id使用response.authResponse.userID獲得用戶ID的

例子:

FB.login(function(response) { 
     if (response.authResponse) { 

     console.log(response.authResponse.userID); // debugg! 

     $('#FBProfileImgVar').attr('src', 'https://graph.facebook.com/' + response.authResponse.userID + '/picture?type=large'); 

     } else { 

      alert('User canceled login or did not fully authorize the app.'); 

      } 
    }); 

Demo #1 Demo #2

+0

將response.id更改爲response.authResponse.userID運行良好,謝謝,編輯了您的答案。 – Relm

0

你說,這是工作,如果你輸入一個數字,所以只是一個想法來到我的腦海裏,什麼是錯的:

如果您正在使用

var response.id = 4; 

它不會工作。 http://jsfiddle.net/8uSjj/1/

var可能不包含char「。」

var responseid = 4; 

它會工作http://jsfiddle.net/8uSjj/2/

希望它能幫助。

+0

這有效,但由於它是預定義的,所以不能達到上述問題的目的。如果是這種情況,它會做到這一點。 $('#FBProfileImgVar')。attr('src','https://graph.facebook.com/4/picture?type=large'); – Relm

相關問題