从客户端获取ServerDateTime

时间:2015-07-31 07:11:37

标签: javascript c# asp.net

我有一个javascript客户端功能来设置DateTime功能。

function SetDateTime()
{
  var presentDate = new Date();
}

相反,无论何时调用SetDateTime()函数,我都希望从服务器端获取日期。

<。>在.cs中我有这个方法来返回服务器时间。

        [System.Web.Services.WebMethod]
        protected static string GetCurrentTime() 
        {
           HiddenField hdnCurrentDateTime = new HiddenField();
           hdnCurrentDateTime.Value =  DateTime.Now.ToString();
           return hdnCurrentDateTime.Value;
        }

如何从客户端javascript访问此方法?谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

使用ajax调用我们可以像这样调用webmethod

$。AJAX({             键入:&#34;帖子&#34;,
            url:&#34; pagename / GetCurrentTime&#34;,
            contentType:&#34; application / json&#34;,
            dataType:&#34; json&#34;,
            成功:功能(数据){
                警报(数据);
                }
            }         });

答案 1 :(得分:0)

由于您的方法是 static 并且假设您的页面中有 hdDate ,请尝试以下操作:

onclick
相关问题