使用EventListener调用函数

时间:2016-05-22 17:25:25

标签: 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。当我登录时,我得到一个空白页面,在我的控制台中,我没有得到任何错误,只有:

但是如何才能弹出共享功能呢?

1 个答案:

答案 0 :(得分:-1)

在上面添加此脚本

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