2012-05-21 67 views
0

首先感謝您閱讀本文。我在使用JavaScript獲取Linkedin登錄API提供的數據時遇到了一些麻煩。下面是該腳本:LinkedIn API的結果通過javascript訪問

<script type="text/javascript"> 
    function onLinkedInAuth() { 
    IN.API.Profile("me").fields(["firstName","lastName","headline","summary","location","educations","skills"]).result(displayProfiles); 
    } 

function displayProfiles(profiles) { 
    member = profiles.values[0]; 
    document.getElementById("name").value = member.firstName +" "+ member.lastName; 
    document.getElementById("pos").value = member.headline; 
    document.getElementById("city").value = member.location.name; 
    document.getElementById("sum").value = member.summary; 

    var i=0; 
     do { 

      var oldHTML = document.getElementById('para').innerHTML; 
      var newHTML = oldHTML + "<tr><td>" + member.educations.values[i].schoolName + "</td></tr>"; 
      document.getElementById('para').innerHTML = newHTML; 
     i++; 
     } 
     while(i<=1); 
     var v=0; 
     do { 
      var oldHTML = document.getElementById('tara').innerHTML; 
      var newHTML = oldHTML + "<tr><td>" + member.skills.values[v].skill.name + "</td></tr>"; 
      document.getElementById('tara').innerHTML = newHTML; 
     v++; 
     } 
     while(member.skills.values[v].skill.name); 

    document.getElementById("educ").value = member.educations.values[1].schoolName; 
    document.getElementById("skills").value = member.skills.values[0].skill.name; 
} 
</script> 

這是一個非常基本的腳本來獲取用戶的相關信息,並就中,用戶的教育和專業背景。問題是member.educations.values [i] .schoolName和member.skills.values [v] .skill.name可以有多個值,我想將它們全部收集起來。

只要指定的字段不爲空,但它會輸出一個錯誤,說member.skills.values [v]是未定義的,並且它不運行第二個循環。

我知道這個錯誤是非常基本的,但我在JavaScript中並不是那麼棒。

感謝您的幫助,祝您有美好的一天!

回答

1

您應該檢查返回值的長度,然後根據需要循環遍歷它們。沿線的東西:

var educations = member.educations; 
if(educations._total > 0) { 
    for(var i = 0; i < educations._total; i++) { 
    document.getElementById("educ").value += (i > 0) ? ', ' : ''; 
    document.getElementById("educ").value += educations.values[i].schoolName; 
    } 
} 
+0

你是最棒的。非常感謝! –

+0

歡迎您! –