Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "JqueryCSMethodForm. aspx. cs" Inherits = "JQuerWeb. JqueryCSMethodForm" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script type = "text/javascript" src = "JS/jquery-1.4.2.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Input [type = 'click'] [value = 'getdate']"). click (function (){
$. Ajax ({
Type: "post ",
Url: "JqueryCSMethodForm. aspx/GetNowDate ",
Datatype: "json ",
ContentType: "application/json; charset = UTF-8 ",
Success: function (data ){
$ ("Input # showTime"). val (eval ('+ data. d +') [0]. nowtime );
},
Error: function (XMLHttpRequest, textStatus, errorThrown ){
Alert (errorThrown );
}
});
});
$ ("Input [type = 'click'] [value = 'getonedaylater ']"). click (function (){
$. Ajax ({
Type: "post ",
Url: "JqueryCSMethodForm. aspx/GetOneDayLate ",
Data: "{days: 1 }",
Datatype: "json ",
ContentType: "application/json; charset = UTF-8 ",
Success: function (data ){
$ ("Input # showTime"). val (eval ('+ data. d +') [0]. nowtime );
},
Error: function (XMLHttpRequest, textStatus, errorThrown ){
Alert (errorThrown );
}
});
});
});
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input type = "button" value = "GetDate"/>
<Input type = button value = "GetOneDayLater"/>
<Input type = "text" id = "showTime"/>
</Div>
</Form>
</Body>
</Html>
CS code:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. Services;
Namespace JQuerWeb
{
Public partial class JqueryCSMethodForm: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
[WebMethod]
Public static String GetNowDate ()
{
Return "[{\" nowtime \ ": \" "+ DateTime. Now. tow.datestring () +" \ "}]";
}
[WebMethod]
Public static String GetOneDayLate (Int32 days)
{
Return "[{\" nowtime \ ": \" "+ DateTime. Now. AddDays (days). tow.datestring () +" \ "}]";
}
}
}
Note:
(1) url format + "/method name"
(2) contentType: "application/json; charset = UTF-8", which must have
(3) The returned data type is json.
(4) data: "{days: 1}", passing Parameters
(5) The background method must be public static and must be modified using the [WebMethod] feature.