1

I Have a javascript client side function to set the DateTime function.

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

Instead I like to get the date from server side whenever SetDateTime() function is called.

in .cs I have this method to return server time.

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

How to access this method from client side javascript? Thank you for your time.

user4895544
  • 103
  • 1
  • 11
  • make an ajax request – Sajeetharan Jul 31 '15 at 07:14
  • you can use `$.ajax({url: 'GetCurrentTime'})` thats all – Nilesh Jul 31 '15 at 07:15
  • 1
    [System.Web.Services.WebMethod] - have you seen that attribute? That's SOAP method, you can't just call it like that. Here is example of calling a SOAP web service question: http://stackoverflow.com/questions/124269/simplest-soap-example – Nikolay Jul 31 '15 at 07:17
  • Why not? its a web method and right candidate for an ajax call. – Nilesh Jul 31 '15 at 07:20
  • I've added link to a similar question. Try comparing the code from that answer with yours.. Am I missing something? – Nikolay Jul 31 '15 at 07:21

2 Answers2

0

using ajax call we can call webmethod like this

$.ajax({ type: "Post",
url: "pagename/GetCurrentTime",
contentType: "application/json",
dataType: "json",
success: function (data) {
alert(data);
}
} });

0

As your method is static and assuming you have hdDate in your page, try this:

    <asp:HiddenField ID="hdDate" runat="server" 
value="<%# YourClassName.GetCurrentTime()%>" />
Reza Ahmadi
  • 357
  • 3
  • 12