【问题标题】:How to add input field to Yammer api query如何将输入字段添加到 Yammer api 查询
【发布时间】:2017-04-12 15:58:11
【问题描述】:

我是一个样板程序员,所以对脚本如何深入工作的技术不是很熟练。

我有一个用于从 Yammer 中提取用户信息的 yammer api:

<!DOCTYPE html>
<html>
  <head>  
  </head> 
<body>

<span id="yammer-login"></span>

<script type="text/javascript" data-app-id="bdlZbJHCm1RY8pMUbuoBlQ" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script>

<script>
yam.getLoginStatus(
  function (response) {
      var result = 1625803434;

    if (response.authResponse) {
      console.log("logged in");
      yam.platform.request({
        url: "users/"+result+".json",     
        method: "GET",
        data: {    //use the data object literal to specify parameters, as documented in the REST API section of this developer site
          "User_Id": result,
        },
        success: function (user) { //print message response information to the console
        str = JSON.stringify(user, null, 4); // (Optional) beautiful indented output.
        document.write(str);
        },
        error: function (user) {
          alert("There was an error with the request.");
        }
      });
    }
    else {
      alert("not logged in")
    }
  }

);

</script>

</body>
</html>

我必须使用结果变量将 Yammer id 硬编码到其中。

我尝试添加一个输入框,然后 yam.getLoginStatus 失败: alert("请求出错。");

<!DOCTYPE html>
<html>
  <head>  
  </head> 
<body>

<form id="frm1" action="#" method="post">
  Yammer ID: <input type="text" name="y_id"><br><br>
  <input type="submit" onclick="yam_id()" value="Submit">
</form> 

<span id="yammer-login"></span>

<script type="text/javascript" data-app-id="bdlZbJHCm1RY8pMUbuoBlQ" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script>

<script>
yam.getLoginStatus(
  function (response) {
      var result = 1625803434;
    frm_i=document.getElementById("frm1");
    result=frm_i.elements[0].value;
//    alert (result);

    if (response.authResponse) {
      console.log("logged in");
      yam.platform.request({
        url: "users/"+result+".json",     
        method: "GET",
        data: {    //use the data object literal to specify parameters, as documented in the REST API section of this developer site
          "User_Id": result,
        },
        success: function (user) { //print message response information to the console
        str = JSON.stringify(user, null, 4); // (Optional) beautiful indented output.
        document.write(str);
          //console.dir(user);
        },
        error: function (user) {
          alert("There was an error with the request.");
        }
      });
    }
    else {
      alert("not logged in")
    }
  }

);
</script>

有谁知道如何简单地为这个脚本添加一个输入框?

【问题讨论】:

    标签: javascript user-input yammer


    【解决方案1】:

    这是我为你准备的两个例子。

    一是表单提交,二是警报提示。在这两种情况下,您都需要将值传递给输入,否则函数将无法运行。

    将您的身份验证放入authUser() 函数中。运行示例代码以查看它的实际效果。

    // Your auth function
    function authUser(id) {
      // Lets validate
      if (id.length === 0) {
        alert('Please enter your data, missing id');
        return false;
      }
      
      // ur ajax/xhr request here
      
      // Just console log so you can see the data
      console.log(id);
    }
    
    // Example 1
    
    document.getElementById('login-action2').addEventListener('click', function() {
      var id = prompt("Please enter your auth credentials/id");
      
      // then pass your id to your auth function
      authUser(id);
    });
    
    
    // Example 2
    
    function formSubmit() {
      var id = document.getElementById('login-action1').value;
    
      
      authUser(id);
      
      // Prevent form from auto submitting
      return false;
    }
    <button id="login-action2" type="button">Log me in</button>
    
    <form action="form-login" onSubmit="return formSubmit();">
    
      <fieldset>
      <input id="login-action1" type="text" placeholder="enter auth credentials/id" value="" />
      <input type="submit"  value="Submit" />
      </fieldset>
    
    </form>

    【讨论】:

    • 感谢您的更新。我可以得到一个输入,问题是将输入传递给 yam.getLoginStatus( 在它运行之前。目前它在从输入框接收到任何输入之前运行。
    • 你读过developer.yammer.com/docs/js-sdk 吗?您缺少这段代码:yam.connect.loginButton('#yammer-login', function (resp) { if (resp.authResponse) { document.getElementById('yammer-login').innerHTML = 'Welcome t Yammer!'; }
    • 嗨,我可以登录没问题,我只需要知道如何将我的输入 yammer id 传递给 yammer api 脚本。如果我在结果变量中硬编码 yammer id,这一切都可以运行并且工作正常。
    • 所以你需要一个已经登录的用户数据。你试过这个代码吗? yam.getLoginStatus( function(response) { if (response.authResponse) { alert("logged in"); console.dir(response); //print user information to the console } else { //authResponse = false if the user is not logged in, or is logged in but hasn't authorized your app yet alert("logged out"); } } ); respresp.authResponse 的控制台输出是什么?没有id吗?
    • 另一个提示是让您检查localStorage 是否有id,有吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多