我真的非常希望有人能帮助我解决这个问题。我整个周末都在努力让代码工作,我向老板保证它会工作到周一早上。
更新
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<!--<script type="text/javascript" src="http://code.jquery.com/jquery-1.5b1.js"></script>-->
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: my_API_key
authorize: true
onLoad: shareContent
</script>
</head>
<body>
<script type="in/Login"></script>
<script>
// Setup an event listener to make an API call once auth is complete
function onLinkedInLoad() {
IN.Event.on(IN, "auth", shareContent);
}
// Handle the successful return from the API call
function onSuccess(data) {
console.log(data);
}
// Handle an error response from the API call
function onError(error) {
console.log(error);
}
// Use the API call wrapper to share content on LinkedIn
function shareContent() {
// Build the JSON payload containing the content to be shared
var payload = {
"comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG",
"visibility": {
"code": "anyone"
}
}
IN.API.Raw("/people/~/shares?format=json")
.method("POST")
.body(JSON.stringify(payload))
.result(onSuccess)
.error(onError);
}
</script>
</body>
</html>
当我运行此代码时,我得到“使用 linkedin 登录”按钮。当我点击此处时,我会登录 LinkedIn。当我登录时,我得到一个空白页面,并且在我的控制台中我没有得到任何错误,只有:
但是如何让分享功能弹出呢?
最佳答案
在上面添加此脚本
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: My_API_Key_without_quotes
onLoad: Linkedin.init
</script>
并将您的 api 初始化移至此处
编辑(这仍然有错误,但现在应共享请求):
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript">
function addListeners() {
if(window.addEventListener) {
document.getElementById('mybtn').addEventListener("click", shareContent, false);
} else if(window.attachEvent) {
document.getElementById('mybtn').attachEvent("onclick", shareContent);
}
}
// Setup an event listener to make an API call once auth is complete
function onLinkedInLoad() {
IN.Event.on(IN, "auth", shareContent);
}
// Handle the successful return from the API call
function onSuccess(data) {
console.log(data);
}
// Handle an error response from the API call
function onError(error) {
console.log(error);
}
// Use the API call wrapper to share content on LinkedIn
function shareContent() {
// Build the JSON payload containing the content to be shared
var payload = {
"comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG",
"visibility": {
"code": "anyone"
}
};
IN.API.Raw("/people/~/shares?format=json")
.method("POST")
.body(JSON.stringify(payload))
.result(onSuccess)
.error(onError);
}
document.addEventListener("DOMContentLoaded", addListeners);
</script>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: My_API_Key_without_quotes
authorize: true
onLoad: onLinkedInLoad
</script>
</head>
<body>
<button id="mybtn">Try Me</button>
</body>
</html>
关于javascript - 使用 EventListener 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37377370/