![](/img/trans.png)
[英]PHP LinkedIn API integration with Javascript LinkedIN API
[英]Javascript - LinkedIn API
我正在嘗試使用JavaScript中的LinkedIn API來檢索用戶字段。
我希望使用AJAX將這些信息作為變量傳遞給另一個頁面。
該代碼可以正常工作,但是當用戶的字段為空時會出現問題。
例如,如果我轉到我的LinkedIn個人資料並刪除我的當前工作,則代碼將停止工作,並且不會傳遞任何變量,因為此字段positions.values[0].title
為空。
刪除整行document.getElementById("imgTitle").src = member.positions.values[0].title;
將使它再次工作。
我的問題是如何處理空字段,並使用AJAX將這些變量傳遞為""
或null
?
<!DOCTYPE html>
<html>
<head>
<script src="assets/js/scripts.js"></script>
<script src="js/jquery.js"></script>
<script type="text/javascript" src="//platform.linkedin.com/in.js">
api_key: [apikey]
onLoad: onLinkedInLoad
authorize: true
lang: [LANG_LOCALE]
</script>
<script type="text/javascript">
function onLinkedInLoad() {
IN.Event.on(IN, "auth", onLinkedInAuth);
}
function onLinkedInAuth() {
IN.API.Profile("me")
.fields(
"firstName",
"lastName",
"industry",
"location:(name)",
"picture-url",
"headline",
"summary",
"num-connections",
"public-profile-url",
"distance",
"positions",
"email-address",
"educations",
"date-of-birth"
)
.result(displayProfiles)
.error(displayProfilesErrors);
}
function displayProfiles(profiles) {
member = profiles.values[0];
alert(JSON.stringify(member));
document.getElementById("lblName").innerHTML = member.firstName + " " + member.lastName + "<br/> Location is " + member.location.name;
document.getElementById("imgProfile").src = member.pictureUrl;
document.getElementById("imgTitle").src = member.positions.values[0].title;
document.getElementById("lblProfile").innerHTML = member.publicProfileUrl;
document.getElementById("lblEmail").innerHTML = member.emailAddress;
var firstName = member.firstName;
var emailAddress = member.emailAddress;
$.ajax({
type: "POST",
url: "heya.php",
data: {firstname: firstName, email: emailAddress},
dataType: 'text',
success: function (data) {
alert(data);
}
});
}
function displayProfilesErrors(error) {
profilesDiv = document.getElementById("profiles");
profilesDiv.innerHTML = error.message;
console.log(error);
}
</script>
</head>
<body>
<script type="in/Login">
</script>
<br/>
<b>Get Linkedin LoggedIn User Details</b>
<table>
<tr>
<td>Name:</td>
<td><label id="lblName"/></td>
</tr>
<tr>
<td>Email:</td>
<td><label id="lblEmail"/></td>
</tr>
<tr>
<td>Profile Url:</td>
<td><label id="lblProfile"/></td>
<td>Title:</td>
<td><label id="lblTitle"/></td>
</tr>
<tr>
<td>Profile Image:</td>
<td><img id="imgProfile"/></td>
</tr>
</table>
<div id="profiles">
</div>
</body>
</html>
不知道是否有更好的方法,但是我使用了Lodash庫並按如下方式解決了我的問題:
if(_.has(profile, 'positions.values[0].title') == true){
var title = profile.positions.values[0].title;
}else{
var title = "";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.