2017-04-12 52 views
-1

我是一個鍋爐板程序員,所以對腳本的工作方式沒有太多技術上的深入。如何添加輸入字段到Yammer API查詢

我具有用於從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> 

有誰知道如何簡單地爲這個腳本添加一個輸入框?

回答

0

以下是我爲您準備的兩個示例。

一個是表單提交,其次是提示提示。在這兩種情況下,您都需要將值傳遞給輸入,否則函數將無法運行。

將您的驗證放入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>

+0

謝謝你的更新。 。 我可以得到一個輸入,這個問題正在通過輸入yam.getLoginStatus( ,然後再運行 在它從輸入框中接收到的任何輸入之前運行時刻 – hrhsii

+0

你讀過https://開頭開發商.yammer.com/docs/js-sdk? 您錯過這段代碼: 'yam.connect.loginButton('#yammer-login',function(resp){ if(resp.authResponse){ document .getElementById('yammer-login')。innerHTML ='Welcome t Yammer!'; }' – loelsonk

+0

嗨, 我可以登錄沒有問題,我只需要知道如何將我輸入的yammer id傳遞給yammer api腳本。 它全部運行並正常工作,如果我在結果變量中硬編碼yammer id。 – hrhsii

相關問題