jquery Asynchronous Call page back-end method & #8207;(asp.net) _jquery

Source: Internet
Author: User
Tags datetime getdate
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
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.