Copy Code code 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 ">
<title></title>
<script type= "Text/javascript" src= "Js/jquery-1.4.2.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("input[type= ' button '][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= ' button '][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>
<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>
CS Code:
Copy Code code 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.ToShortDateString () +" \ "}]";
}
[WebMethod]
public static String getonedaylate (Int32 days)
{
Return "[{\ nowtime\]: \" "+ DateTime.Now.AddDays (days). ToShortDateString () + "\"}];
}
}
}
Note the point:
(1) The filling format of the URL url+ "/method name"
(2) ContentType: "Application/json; Charset=utf-8 ", this must have
(3) The type of the returned data is JSON
(4) Data: "{days:1}", transfer of parameters
(5) The method in the background must be public static and also have [WebMethod] attribute modification