2013-11-22 83 views
0

在我的Rails應用程序(考試系統)中,我需要使用服務器時間進行驗證。如何使用Ajax請求獲取服務器時間?

在按鈕上點擊函數,獲取服務器時間並驗證相同。現在我使用Time.now方法獲取它,但它無法動態獲取服務器時間。意思是,它一直顯示在頁面加載期間獲取的服務器時間。

如何使用Ajax請求來完成上述操作?現在我的應用程序處於開發模式。所以Client和Server機器是一樣的。所以,請給我一些相同的解決方案。

回答

0

調用每秒獲取服務器時間和顯示的ajax函數。 因此,服務器時間每秒刷新一次。您需要每秒調用一次該函數。

window.setInterval(function(){ 
     /// call your function here 
    //function to get server time and display. 
function ShowData(); 
    }, 1000) 
// 
suppose div to display time.. 

<div id="datePanel"></div> 

//function to get server tym from servertime.php 
<script language="JavaScript"> 
ajax = new XMLHttpRequest(); 

function ShowData() 
{ 
    ajax.open("GET", "servertime.php", true); 
    ajax.onreadystatechange = function() 
    { 
    if (ajax.readyState == 4) 
    {  
     document.getElementById("DataPanel").innerHTML = ajax.responseText; 
    } 
    }  
    ajax.send(null); 
} 
</script>