Ajax obtains the current server time

Source: Internet
Author: User

------------------------------ Webservice1.asmx ----------------------------------

// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.

[System. Web. Script. Services. scriptservice]
Public class webservice1: system. Web. Services. WebService
{
[Webmethod]
Public String helloworld ()
{
Return "Hello World ";
}

[Webmethod]
Public String getdate ()
{
Return datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS ");
}

}

------------------------------------Htmlpage1.htm ---------------------------------------

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<SCRIPT src = "JS/jquery1.7.js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Function (){
Function getdate (){
$. Ajax ({
Type: "Post", // method used when the client sends a request to the server
Contenttype: "application/JSON", // specifies the type of content that the client sends to the server and the type of content that the server returns to the client in JSON format.
URL: "webservice1.asmx/getdate", // specifies the method on which the client sends the request
Data: "{}", // specify the parameters that are sent along with the request to the server
Success: function (result) {// callback function executed after the client successfully calls the server method.
$ ('# Mydiv'). Text (result. D );
}
})
}
Setinterval (getdate, 1000 );
})
</SCRIPT>
</Head>
<Body>
<Div id = "mydiv"> </div>
<Input id = "button1" type = "button" value = "Get server time"/>
</Body>
</Html>


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.