javascript - 使用 EventListener 调用函数

标签 javascript api

我真的非常希望有人能帮助我解决这个问题。我整个周末都在努力让代码工作,我向老板保证它会工作到周一早上。

更新

<!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/

相关文章:

javascript - 为什么这个 JQuery 代码在移动设备上会出错?

javascript - 文本搜索——过滤两个不同对象/数组中的两个词

javascript - Node.js 无法在 api 中正确显示结果状态

ios - 是一种在ios 上阅读Apple API 内部实现的方法吗?

api - 如何在 flutter 中显示从 API 响应接收到的图像?

ios - 使用 Apple Music API 创建播放列表?

javascript - 使用最少的 JQuery 显示放大图像的最佳方式是什么?

javascript - 在 2 个 div 之间画一条线

javascript - AngularJs 无法从本地加载 JSON 文件 "Maven Project"

c++ - 使用 Catboost C 评估库 API